File Explorer
lv_file_explorer
provides an API to browse the contents of the file
system. lv_file_explorer
only provides the file browsing function,
but does not provide the actual file operation function. In other words,
you can't click a picture file to open and view the picture like a PC.
lv_file_explorer
will tell you the full path and name of the
currently clicked file. The file operation function needs to be
implemented by the user.
The file list in lv_file_explorer
is based on
lv_table, and the quick access bar is based on
lv_list. Therefore, care should be taken to ensure
that lv_table and lv_list are
enabled.
中文
lv_file_explorer
提供API让我们可以浏览文件系统中的内容。lv_file_explorer
只提供了文件浏览功能,并不提供实际的文件操作功能,也就是说,不能像PC那样点击一个图片文件就可以打开查看该图片。lv_file_explorer
会告诉您当前点击的文件的完整路径和名称,文件操作功能需要用户自己实现。
lv_file_explorer
中的文件列表基于 lv_table
实现,快速访问栏基于 lv_list
实现。因此,要注意确保使能了 lv_table
和 lv_list
。
Usage
Enable LV_USE_FILE_EXPLORER
in lv_conf.h
.
First use lv_file_explorer_create(lv_screen_active()) to create a file explorer, The default size is the screen size. After that, you can customize the style like widget.
中文
在 lv_conf.h
中打开 LV_USE_FILE_EXPLORER
。
首先,使用 lv_file_explorer_create(lv_screen_active()) 函数创建一个文件浏览器,默认大小为屏幕大小,之后可以像组件那样自定义样式。
Quick access
The quick access bar is optional. You can turn off
LV_FILE_EXPLORER_QUICK_ACCESS
in lv_conf.h
so that the quick
access bar will not be created. This can save some memory, but not much.
After the quick access bar is created, it can be hidden by clicking the
button at the top left corner of the browsing area, which is very useful
for small screen devices.
You can use lv_file_explorer_set_quick_access_path(file_explorer, LV_FILE_EXPLORER_QA_XX, "path") to set the path of the quick access bar. The items of the quick access bar are fixed. Currently, there are the following items:
LV_FILE_EXPLORER_QA_HOME
LV_FILE_EXPLORER_QA_MUSIC
LV_FILE_EXPLORER_QA_PICTURES
LV_FILE_EXPLORER_QA_VIDEO
LV_FILE_EXPLORER_QA_DOCS
LV_FILE_EXPLORER_QA_MNT
LV_FILE_EXPLORER_QA_FS
中文
快速访问栏是可选的,您可以在 lv_conf.h
中关闭
LV_FILE_EXPLORER_QUICK_ACCESS
,这样快速访问栏就不会被创建出来,这能节省一些内存,但并不是很多。快速访问栏被创建出来之后,可以通过点击浏览区域顶部左上角的按钮隐藏起来,这对于小屏幕设备非常有用。
可以通过 lv_file_explorer_set_quick_access_path(file_explorer, LV_FILE_EXPLORER_QA_XX, "path") 设置快速访问栏的路径,快速访问栏的项目是固定的,目前有以下项目:
LV_FILE_EXPLORER_QA_HOME
LV_FILE_EXPLORER_QA_MUSIC
LV_FILE_EXPLORER_QA_PICTURES
LV_FILE_EXPLORER_QA_VIDEO
LV_FILE_EXPLORER_QA_DOCS
LV_FILE_EXPLORER_QA_MNT
LV_FILE_EXPLORER_QA_FS
Sort
You can use lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_XX) to set sorting method. There are the following sorting methods:
LV_EXPLORER_SORT_NONE
LV_EXPLORER_SORT_KIND
You can customize the sorting. Before custom sort, please set the
default sorting to LV_EXPLORER_SORT_NONE
. The default is
LV_EXPLORER_SORT_NONE
.
中文
可以通过 lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_XX) 设置排序方式,有以下排序方式:
LV_EXPLORER_SORT_NONE
LV_EXPLORER_SORT_KIND
您可以自定义排序规则,在这之前请先将排序规则设置为
LV_EXPLORER_SORT_NONE
然后在 LV_EVENT_READY
事件中处理。默认的排序规则是 LV_EXPLORER_SORT_NONE
Event
LV_EVENT_READY
sent when a directory is opened. You can customize the sort.LV_EVENT_VALUE_CHANGED
sent when an item(file) in the file list is clicked.
You can use lv_file_explorer_get_cur_path()
to get the current path
and lv_file_explorer_get_sel_fn()
to get the name of the currently
selected file in the event processing function. For example:
static void file_explorer_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
char * cur_path = lv_file_explorer_get_cur_path(obj);
char * sel_fn = lv_file_explorer_get_sel_fn(obj);
LV_LOG_USER("%s%s", cur_path, sel_fn);
}
}
You can also save the obtained path and file name into an array through functions such as strcpy and strcat for later use.
中文
当打开一个目录后会发送
LV_EVENT_READY
事件。您可以在这里自定义排序规则。当文件列表中的项目(文件)被点击时会发送
LV_EVENT_VALUE_CHANGED
事件。
可以在事件处理函数中通过 lv_file_explorer_get_cur_path()
获取当前所在的路径,通过 lv_file_explorer_get_sel_fn()
获取当前选中的文件的名称。比如:
static void file_explorer_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
char * cur_path = lv_file_explorer_get_cur_path(obj);
char * sel_fn = lv_file_explorer_get_sel_fn(obj);
LV_LOG_USER("%s%s", cur_path, sel_fn);
}
}
您还可以将获取到的 路径 和 文件名称 通过例如 strcpy 和 strcat 函数保存到一个数组中,方便后续使用。
Example
Simple File Explorer
C code
View on GitHub
#include "../../lv_examples.h"
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
#include <stdlib.h>
#include <string.h>
static void file_explorer_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
const char * cur_path = lv_file_explorer_get_current_path(obj);
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
uint16_t path_len = strlen(cur_path);
uint16_t fn_len = strlen(sel_fn);
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
strcpy(file_info, cur_path);
strcat(file_info, sel_fn);
LV_LOG_USER("%s", file_info);
}
else LV_LOG_USER("%s%s", cur_path, sel_fn);
}
}
void lv_example_file_explorer_1(void)
{
lv_obj_t * file_explorer = lv_file_explorer_create(lv_screen_active());
lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_KIND);
#if LV_USE_FS_WIN32
lv_file_explorer_open_dir(file_explorer, "D:");
#if LV_FILE_EXPLORER_QUICK_ACCESS
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
#endif
#else
/* linux */
lv_file_explorer_open_dir(file_explorer, "A:/");
#if LV_FILE_EXPLORER_QUICK_ACCESS
char * envvar = "HOME";
char home_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(home_dir, "A:");
/* get the user's home directory from the HOME environment variable*/
strcat(home_dir, getenv(envvar));
LV_LOG_USER("home_dir: %s\n", home_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, home_dir);
char video_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(video_dir, home_dir);
strcat(video_dir, "/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, video_dir);
char picture_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(picture_dir, home_dir);
strcat(picture_dir, "/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, picture_dir);
char music_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(music_dir, home_dir);
strcat(music_dir, "/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, music_dir);
char document_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(document_dir, home_dir);
strcat(document_dir, "/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, document_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "A:/");
#endif
#endif
lv_obj_add_event(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
}
#endif
import fs_driver
import os
LV_FILE_EXPLORER_QUICK_ACCESS = True
LV_USE_FS_WIN32 = False
LV_FILE_EXPLORER_PATH_MAX_LEN = 128
def file_explorer_event_handler(e) :
code = e.get_code()
obj = e.get_target_obj()
if code == lv.EVENT.VALUE_CHANGED:
cur_path = obj.explorer_get_current_path()
sel_fn = obj.explorer_get_selected_file_name()
# print("cur_path: " + str(cur_path), " sel_fn: " + str(sel_fn))
print(str(cur_path) + str(sel_fn))
file_explorer = lv.file_explorer(lv.screen_active())
file_explorer.explorer_set_sort(lv.EXPLORER_SORT.KIND)
if LV_USE_FS_WIN32 :
file_explorer.explorer_open_dir("D:")
if LV_FILE_EXPLORER_QUICK_ACCESS :
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, "C:/Users/Public/Desktop")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, "C:/Users/Public/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_FS_DIR, "D:");
# linux
file_explorer.explorer_open_dir("A:/")
if LV_FILE_EXPLORER_QUICK_ACCESS :
home_dir = "A:" + os.getenv('HOME')
print("quick access: " + home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, home_dir + "/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.PICTURES_DIR, home_dir + "/Pictures")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.MUSIC_DIR, home_dir + "/Music")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.DOCS_DIR, home_dir + "/Documents")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.FS_DIR, "A:/")
file_explorer.add_event(file_explorer_event_handler, lv.EVENT.ALL, None)
Control File Explorer
C code
View on GitHub
#include "../../lv_examples.h"
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
#include <stdlib.h>
#include <string.h>
static void file_explorer_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
const char * cur_path = lv_file_explorer_get_current_path(obj);
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
uint16_t path_len = strlen(cur_path);
uint16_t fn_len = strlen(sel_fn);
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
strcpy(file_info, cur_path);
strcat(file_info, sel_fn);
LV_LOG_USER("%s", file_info);
}
else LV_LOG_USER("%s%s", cur_path, sel_fn);
}
}
static void btn_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
lv_obj_t * file_explorer = lv_event_get_user_data(e);
if(code == LV_EVENT_VALUE_CHANGED) {
if(lv_obj_has_state(btn, LV_STATE_CHECKED))
lv_obj_add_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
else
lv_obj_remove_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
}
}
static void dd_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * dd = lv_event_get_target(e);
lv_obj_t * fe_quick_access_obj = lv_event_get_user_data(e);
if(code == LV_EVENT_VALUE_CHANGED) {
char buf[32];
lv_dropdown_get_selected_str(dd, buf, sizeof(buf));
if(strcmp(buf, "NONE") == 0) {
lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_NONE);
}
else if(strcmp(buf, "KIND") == 0) {
lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_KIND);
}
}
}
void lv_example_file_explorer_2(void)
{
lv_obj_t * file_explorer = lv_file_explorer_create(lv_screen_active());
#if LV_USE_FS_WIN32
lv_file_explorer_open_dir(file_explorer, "D:");
#if LV_FILE_EXPLORER_QUICK_ACCESS
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
#endif
#else
/* linux */
lv_file_explorer_open_dir(file_explorer, "A:/");
#if LV_FILE_EXPLORER_QUICK_ACCESS
char * envvar = "HOME";
char home_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(home_dir, "A:");
/* get the user's home directory from the HOME environment variable*/
strcat(home_dir, getenv(envvar));
LV_LOG_USER("home_dir: %s\n", home_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, home_dir);
char video_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(video_dir, home_dir);
strcat(video_dir, "/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, video_dir);
char picture_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(picture_dir, home_dir);
strcat(picture_dir, "/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, picture_dir);
char music_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(music_dir, home_dir);
strcat(music_dir, "/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, music_dir);
char document_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(document_dir, home_dir);
strcat(document_dir, "/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, document_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "A:/");
#endif
#endif
lv_obj_add_event(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
/*Quick access status control button*/
lv_obj_t * fe_quick_access_obj = lv_file_explorer_get_quick_access_area(file_explorer);
lv_obj_t * fe_header_obj = lv_file_explorer_get_header(file_explorer);
lv_obj_t * btn = lv_button_create(fe_header_obj);
lv_obj_set_style_radius(btn, 2, 0);
lv_obj_set_style_pad_all(btn, 4, 0);
lv_obj_align(btn, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_LIST);
lv_obj_center(label);
lv_obj_add_event(btn, btn_event_handler, LV_EVENT_VALUE_CHANGED, fe_quick_access_obj);
/*Sort control*/
static const char * opts = "NONE\n"
"KIND";
lv_obj_t * dd = lv_dropdown_create(fe_header_obj);
lv_obj_set_style_radius(dd, 4, 0);
lv_obj_set_style_pad_all(dd, 0, 0);
lv_obj_set_size(dd, LV_PCT(30), LV_SIZE_CONTENT);
lv_dropdown_set_options_static(dd, opts);
lv_obj_align(dd, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_add_event(dd, dd_event_handler, LV_EVENT_VALUE_CHANGED, file_explorer);
}
#endif
import fs_driver
import os
LV_FILE_EXPLORER_QUICK_ACCESS = True
LV_USE_FS_WIN32 = False
LV_FILE_EXPLORER_PATH_MAX_LEN = 128
def file_explorer_event_handler(e):
code = e.get_code()
obj = e.get_target_obj()
if code == lv.EVENT.VALUE_CHANGED:
cur_path = obj.explorer_get_current_path()
sel_fn = obj.explorer_get_selected_file_name()
print(str(cur_path) + str(sel_fn))
def button_event_handler(e,fe_quick_access_obj):
code = e.get_code()
button = e.get_target_obj()
# lv_obj_t * file_explorer = lv_event_get_user_data(e);
if code == lv.EVENT.VALUE_CHANGED :
if button.has_state(lv.STATE.CHECKED) :
fe_quick_access_obj.add_flag(lv.obj.FLAG.HIDDEN)
else :
fe_quick_access_obj.remove_flag(lv.obj.FLAG.HIDDEN)
def dd_event_handler(e, file_explorer):
code = e.get_code()
dd = e.get_target_obj()
# fe_quick_access_obj = lv_event_get_user_data(e);
if code == lv.EVENT.VALUE_CHANGED :
buf = bytearray(32)
lv.dropdown.get_selected_str(dd,buf,len(buf))
if buf[:4] == b"NONE":
# print("set sort to NONE")
file_explorer.explorer_set_sort(lv.EXPLORER_SORT.NONE)
elif buf[:4] == b"KIND" :
# print("set sort to KIND")
file_explorer.explorer_set_sort(lv.EXPLORER_SORT.KIND)
file_explorer = lv.file_explorer(lv.screen_active())
if LV_USE_FS_WIN32 :
file_explorer.explorer_open_dir("D:")
if LV_FILE_EXPLORER_QUICK_ACCESS :
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, "C:/Users/Public/Desktop")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, "C:/Users/Public/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_FS_DIR, "D:");
# linux
file_explorer.explorer_open_dir("A:/")
if LV_FILE_EXPLORER_QUICK_ACCESS :
home_dir = "A:" + os.getenv('HOME')
print("quick access: " + home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, home_dir + "/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.PICTURES_DIR, home_dir + "/Pictures")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.MUSIC_DIR, home_dir + "/Music")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.DOCS_DIR, home_dir + "/Documents")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.FS_DIR, "A:/")
file_explorer.add_event(file_explorer_event_handler, lv.EVENT.ALL, None)
# Quick access status control button
fe_quick_access_obj = file_explorer.explorer_get_quick_access_area()
fe_header_obj = file_explorer.explorer_get_header()
button = lv.button(fe_header_obj)
button.set_style_radius(2, 0)
button.set_style_pad_all(4, 0)
button.align(lv.ALIGN.LEFT_MID, 0, 0)
button.add_flag(lv.obj.FLAG.CHECKABLE)
label = lv.label(button)
label.set_text(lv.SYMBOL.LIST)
label.center()
button.add_event(lambda evt: button_event_handler(evt,fe_quick_access_obj), lv.EVENT.VALUE_CHANGED, None)
#button.add_event(lambda evt: button_event_handler(evt,file_explorer), lv.EVENT.VALUE_CHANGED, None)
# Sort control
opts = "NONE\nKIND"
dd = lv.dropdown(fe_header_obj)
dd.set_style_radius(4, 0)
dd.set_style_pad_all(0, 0)
dd.set_size(lv.pct(30), lv.SIZE_CONTENT)
dd.set_options_static(opts)
dd.align(lv.ALIGN.RIGHT_MID, 0, 0)
# lv_obj_align_to(dd, button, LV_ALIGN_OUT_RIGHT_MID, 0, 0);
dd.add_event(lambda evt: dd_event_handler(evt,file_explorer), lv.EVENT.VALUE_CHANGED, None)
#dd.add_event(lambda evt: dd_event_handler(evt,fe_quick_access_obj), lv.EVENT.VALUE_CHANGED, None)
Custom sort
C code
View on GitHub
#include "../../lv_examples.h"
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
#include <stdlib.h>
#include <string.h>
static void exch_table_item(lv_obj_t * tb, int16_t i, int16_t j)
{
const char * tmp;
tmp = lv_table_get_cell_value(tb, i, 0);
lv_table_set_cell_value(tb, 0, 2, tmp);
lv_table_set_cell_value(tb, i, 0, lv_table_get_cell_value(tb, j, 0));
lv_table_set_cell_value(tb, j, 0, lv_table_get_cell_value(tb, 0, 2));
tmp = lv_table_get_cell_value(tb, i, 1);
lv_table_set_cell_value(tb, 0, 2, tmp);
lv_table_set_cell_value(tb, i, 1, lv_table_get_cell_value(tb, j, 1));
lv_table_set_cell_value(tb, j, 1, lv_table_get_cell_value(tb, 0, 2));
}
/*Quick sort 3 way*/
static void sort_by_file_kind(lv_obj_t * tb, int16_t lo, int16_t hi)
{
if(lo >= hi) return;
int16_t lt = lo;
int16_t i = lo + 1;
int16_t gt = hi;
const char * v = lv_table_get_cell_value(tb, lo, 1);
while(i <= gt) {
if(strcmp(lv_table_get_cell_value(tb, i, 1), v) < 0)
exch_table_item(tb, lt++, i++);
else if(strcmp(lv_table_get_cell_value(tb, i, 1), v) > 0)
exch_table_item(tb, i, gt--);
else
i++;
}
sort_by_file_kind(tb, lo, lt - 1);
sort_by_file_kind(tb, gt + 1, hi);
}
static void file_explorer_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
const char * cur_path = lv_file_explorer_get_current_path(obj);
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
uint16_t path_len = strlen(cur_path);
uint16_t fn_len = strlen(sel_fn);
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
strcpy(file_info, cur_path);
strcat(file_info, sel_fn);
LV_LOG_USER("%s", file_info);
}
else LV_LOG_USER("%s%s", cur_path, sel_fn);
}
else if(code == LV_EVENT_READY) {
lv_obj_t * tb = lv_file_explorer_get_file_table(obj);
uint16_t sum = lv_table_get_row_cnt(tb);
sort_by_file_kind(tb, 0, (sum - 1));
}
}
void lv_example_file_explorer_3(void)
{
lv_obj_t * file_explorer = lv_file_explorer_create(lv_screen_active());
/*Before custom sort, please set the default sorting to NONE. The default is NONE.*/
lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_NONE);
#if LV_USE_FS_WIN32
lv_file_explorer_open_dir(file_explorer, "D:");
#if LV_FILE_EXPLORER_QUICK_ACCESS
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
#endif
#else
/* linux */
lv_file_explorer_open_dir(file_explorer, "A:/");
#if LV_FILE_EXPLORER_QUICK_ACCESS
char * envvar = "HOME";
char home_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(home_dir, "A:");
/* get the user's home directory from the HOME environment variable*/
strcat(home_dir, getenv(envvar));
LV_LOG_USER("home_dir: %s\n", home_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, home_dir);
char video_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(video_dir, home_dir);
strcat(video_dir, "/Videos");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, video_dir);
char picture_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(picture_dir, home_dir);
strcat(picture_dir, "/Pictures");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, picture_dir);
char music_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(music_dir, home_dir);
strcat(music_dir, "/Music");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, music_dir);
char document_dir[LV_FS_MAX_PATH_LENGTH];
strcpy(document_dir, home_dir);
strcat(document_dir, "/Documents");
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, document_dir);
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "A:/");
#endif
#endif
lv_obj_add_event(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
}
#endif
import fs_driver
import os
LV_FILE_EXPLORER_QUICK_ACCESS = True
LV_USE_FS_WIN32 = False
LV_FILE_EXPLORER_PATH_MAX_LEN = 128
def exch_table_item(tb, i, j):
tmp = tb.get_cell_value(i, 0)
tb.set_cell_value(0, 2, tmp)
tb.set_cell_value(i, 0, tb.get_cell_value(j, 0))
tb.set_cell_value(j, 0, tb.get_cell_value(0, 2))
tmp = tb.get_cell_value(i, 1)
tb.set_cell_value(0, 2, tmp)
tb.set_cell_value(i, 1, tb.get_cell_value(j, 1))
tb.set_cell_value(j, 1, tb.get_cell_value(0, 2))
# Quick sort 3 way
def sort_by_file_kind(tb, lo, hi) :
if lo >= hi:
return;
lt = lo
i = lo + 1
gt = hi
v = tb.get_cell_value(lo, 1)
while i <= gt :
if tb.get_cell_value(i, 1) < v :
lt += 1
i += 1
exch_table_item(tb, lt, i)
elif tb.get_cell_value(i, 1) > v:
gt -= 1
exch_table_item(tb, i, gt)
else :
i += 1
sort_by_file_kind(tb, lo, lt - 1);
sort_by_file_kind(tb, gt + 1, hi);
def file_explorer_event_handler(e) :
code = e.get_code()
obj = e.get_target_obj()
if code == lv.EVENT.VALUE_CHANGED:
cur_path = obj.explorer_get_current_path()
sel_fn = obj.explorer_get_selected_file_name()
print(str(cur_path) + str(sel_fn))
elif code == lv.EVENT.READY :
tb = obj.explorer_get_file_table()
sum = tb.get_row_cnt()
# print("sum: ",sum)
sort_by_file_kind(tb, 0, (sum - 1));
file_explorer = lv.file_explorer(lv.screen_active())
# Before custom sort, please set the default sorting to NONE. The default is NONE.
file_explorer.explorer_set_sort(lv.EXPLORER_SORT.NONE)
file_explorer = lv.file_explorer(lv.screen_active())
if LV_USE_FS_WIN32 :
file_explorer.explorer_open_dir("D:")
if LV_FILE_EXPLORER_QUICK_ACCESS :
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, "C:/Users/Public/Desktop")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, "C:/Users/Public/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
file_explorer.explorer_set_quick_access_path(lv.EXPLORER_FS_DIR, "D:");
# linux
file_explorer.explorer_open_dir("A:/")
if LV_FILE_EXPLORER_QUICK_ACCESS :
home_dir = "A:" + os.getenv('HOME')
print("quick access: " + home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.HOME_DIR, home_dir)
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.VIDEO_DIR, home_dir + "/Videos")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.PICTURES_DIR, home_dir + "/Pictures")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.MUSIC_DIR, home_dir + "/Music")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.DOCS_DIR, home_dir + "/Documents")
file_explorer.explorer_set_quick_access_path(lv.EXPLORER.FS_DIR, "A:/")
file_explorer.add_event(file_explorer_event_handler, lv.EVENT.ALL, None)