From 88ad7366a3eb6bc73bc99078ae13302099b76f09 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 26 Oct 2015 19:48:30 +0100 Subject: Refactored LineEdit and FileBrowser. Renamed all KEY_XYZ event types to KeyXyz. --- plugingui/filebrowser.cc | 359 ++++++++++++++++++++++------------------------- 1 file changed, 168 insertions(+), 191 deletions(-) (limited to 'plugingui/filebrowser.cc') diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index 4b35127..9635c94 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -45,239 +45,216 @@ #include #endif -struct GUI::FileBrowser::private_data { - GUI::LineEdit *lineedit; - GUI::ListBox *listbox; - void (*filesel_handler)(void *, std::string); - void *ptr; - Directory *dir; -#ifdef WIN32 - bool above_root; - bool in_root; -#endif -}; - -static void cancel(void *ptr) -{ - GUI::FileBrowser *fp = (GUI::FileBrowser *)ptr; - fp->hide(); -} - -static void changeDir(void *ptr) +namespace GUI { + +FileBrowser::FileBrowser(Widget *parent) + : Widget(parent) + , dir(Directory::cwd()) + , lbl_path(this) + , lineedit(this) + , listbox(this) + , btn_sel(this) + , btn_esc(this) + , back(":bg.png") { - struct GUI::FileBrowser::private_data *prv = - (struct GUI::FileBrowser::private_data *) ptr; - - GUI::ListBox *lb = prv->listbox; - GUI::LineEdit *le = prv->lineedit; - std::string value = lb->selectedValue(); - GUI::Directory* dir = prv->dir; - -// if(!Directory::isDir(dir->path() + dir->seperator())) { -// return; -// } - - lb->clear(); - - INFO(filebrowser, "Changing path to '%s'\n", - (dir->path() + dir->seperator() + value).c_str()); - #ifdef WIN32 - if(prv->above_root && !value.empty()) { - dir->setPath(value + dir->seperator()); - value.clear(); - prv->above_root = false; - } + above_root = false; #endif - if(value.empty() && !dir->isDir() && GUI::Directory::exists(dir->path())) { - DEBUG(filebrowser, "Selecting file '%s'\n", dir->path().c_str()); - if(prv->filesel_handler) prv->filesel_handler(prv->ptr, dir->path().c_str()); - return; - } + lbl_path.setText("Path:"); - if(!value.empty() && dir->fileExists(value)) { - std::string file = dir->path() + dir->seperator() + value; - DEBUG(filebrowser, "Selecting file '%s'\n", file.c_str()); - if(prv->filesel_handler) prv->filesel_handler(prv->ptr, file); - return; - } + //lineedit.setReadOnly(true); + CONNECT(&lineedit, enterPressedNotifier, this, &FileBrowser::handleKeyEvent); + CONNECT(&listbox, selectionNotifier, + this, &FileBrowser::listSelectionChanged); - std::vector items; + btn_sel.setText("Select"); + CONNECT(&btn_sel, clickNotifier, this, &FileBrowser::selectButtonClicked); -#ifdef WIN32 - if(GUI::Directory::isRoot(dir->path()) && value == "..") { - DEBUG(filebrowser, "Showing partitions...\n"); - GUI::Directory::DriveList entries = dir->drives(); - for(GUI::Directory::DriveList::iterator it = entries.begin(); - it != entries.end(); it++) { - GUI::ListBoxBasic::Item item; - std::string name = (*it).name; - item.name = name; - item.value = name; - items.push_back(item); - } - prv->above_root = true; - } else { -#endif - - if(!value.empty() && !dir->cd(value)) { - DEBUG(filebrowser, "Error changing to '%s'\n", - (dir->path() + dir->seperator() + value).c_str()); - return; - } - - GUI::Directory::EntryList entries = dir->entryList(); - - if(entries.empty()) { - dir->cdUp(); - entries = dir->entryList(); - } - - DEBUG(filebrowser, "Setting path of lineedit to %s\n", - dir->path().c_str()); - le->setText(dir->path()); - - for(GUI::Directory::EntryList::iterator it = entries.begin(); - it != entries.end(); it++) { - GUI::ListBoxBasic::Item item; - std::string name = *it; - item.name = name; - item.value = name; - items.push_back(item); - } -#ifdef WIN32 - } -#endif - lb->addItems(items); -} + btn_esc.setText("Cancel"); + CONNECT(&btn_esc, clickNotifier, this, &FileBrowser::cancelButtonClicked); -static void handleKeyEvent(void *ptr) { - struct GUI::FileBrowser::private_data *prv = - (struct GUI::FileBrowser::private_data *) ptr; - - GUI::ListBox *lb = prv->listbox; - lb->clearSelectedValue(); - GUI::LineEdit *le = prv->lineedit; - - std::string value = le->text(); - if(value.size() > 1 && value[0] == '@') { - DEBUG(filebrowser, "Selecting ref-file '%s'\n", value.c_str()); - if(prv->filesel_handler) { - prv->filesel_handler(prv->ptr, value); - } - return; - } - - prv->dir->setPath(le->text()); - changeDir(ptr); + changeDir(); } -GUI::FileBrowser::FileBrowser(GUI::Widget *parent) - : GUI::Widget(parent), - lbl_path(this), lineedit(this), listbox(this), btn_sel(this), btn_esc(this), - back(":bg.png") +FileBrowser::~FileBrowser() { - prv = new struct GUI::FileBrowser::private_data(); - prv->filesel_handler = NULL; - - prv->dir = new Directory(Directory::cwd()); -#ifdef WIN32 - prv->above_root = false; -#endif - - lbl_path.setText("Path:"); - -// lineedit.setReadOnly(true); - prv->lineedit = &lineedit; - prv->lineedit->registerEnterPressedHandler(handleKeyEvent, prv); - - prv->listbox = &listbox; - CONNECT(&listbox, selectionNotifier, this, &FileBrowser::listSelectionChanged); - - btn_sel.setText("Select"); - CONNECT(&btn_sel, clickNotifier, this, &FileBrowser::selectButtonClicked); - - btn_esc.setText("Cancel"); - CONNECT(&btn_esc, clickNotifier, this, &FileBrowser::cancelButtonClicked); - - changeDir(prv); - - resize(200, 190); } -GUI::FileBrowser::~FileBrowser() +void FileBrowser::setPath(const std::string& path) { - // delete prv->listbox; - delete prv; -} + INFO(filebrowser, "Setting path to '%s'\n", path.c_str()); -void GUI::FileBrowser::setPath(std::string path) -{ - INFO(filebrowser, "Setting path to '%s'\n", path.c_str()); - if(path.empty()) path = Directory::cwd(); + if(!path.empty()) + { + dir.setPath(Directory::pathDirectory(path)); + } + else + { + dir.setPath(Directory::pathDirectory(Directory::cwd())); + } - prv->dir->setPath(Directory::pathDirectory(path)); - prv->listbox->clear(); + listbox.clear(); - changeDir(prv); + changeDir(); } -void GUI::FileBrowser::resize(int w, int h) +void FileBrowser::resize(int w, int h) { - GUI::Widget::resize(w,h); + Widget::resize(w,h); + + int offset = 0; + int brd = 5; // border + int btn_h = 30; - int offset = 0; - int brd = 5; // border - int btn_h = 30; + offset += brd; - offset += brd; + lbl_path.move(0, offset); + lineedit.move(60, offset); - lbl_path.move(0, offset); - lineedit.move(60, offset); + offset += btn_h; - offset += btn_h; + lbl_path.resize(60, btn_h); + lineedit.resize(w - 60 - brd, btn_h); - lbl_path.resize(60, btn_h); - lineedit.resize(w - 60 - brd, btn_h); + offset += brd; - offset += brd; + listbox.move(brd, offset); + listbox.resize(w - 1 - 2*brd, h - btn_h - 2*brd - offset); - listbox.move(brd, offset); - listbox.resize(w - 1 - 2*brd, h - btn_h - 2*brd - offset); + btn_esc.move(brd, h - btn_h - brd); + btn_esc.resize((w - 1 - 2*brd) / 2 - brd / 2, btn_h); - btn_esc.move(brd, h - btn_h - brd); - btn_esc.resize((w - 1 - 2*brd) / 2 - brd / 2, btn_h); + btn_sel.move(brd + w / 2 - brd / 2, h - btn_h - brd); + btn_sel.resize((w - 1 - 2*brd) / 2, btn_h); +} - btn_sel.move(brd + w / 2 - brd / 2, h - btn_h - brd); - btn_sel.resize((w - 1 - 2*brd) / 2, btn_h); +void FileBrowser::repaintEvent(RepaintEvent *e) +{ + Painter p(this); + p.drawImageStretched(0,0, &back, width(), height()); } -void GUI::FileBrowser::registerFileSelectHandler(void (*handler)(void *, - std::string), - void *ptr) +void FileBrowser::listSelectionChanged() { - prv->filesel_handler = handler; - prv->ptr = ptr; + changeDir(); } -void GUI::FileBrowser::repaintEvent(GUI::RepaintEvent *e) +void FileBrowser::selectButtonClicked() { - Painter p(this); - p.drawImageStretched(0,0, &back, width(), height()); + changeDir(); } -void GUI::FileBrowser::listSelectionChanged() +void FileBrowser::cancelButtonClicked() { - changeDir(prv); + cancel(); } -void GUI::FileBrowser::selectButtonClicked() +void FileBrowser::handleKeyEvent() { - changeDir(prv); + listbox.clearSelectedValue(); + + std::string value = lineedit.text(); + if((value.size() > 1) && (value[0] == '@')) + { + DEBUG(filebrowser, "Selecting ref-file '%s'\n", value.c_str()); + fileSelectNotifier(value); + return; + } + + dir.setPath(lineedit.text()); + changeDir(); } -void GUI::FileBrowser::cancelButtonClicked() +void FileBrowser::cancel() { - cancel(this); + hide(); } + +void FileBrowser::changeDir() +{ + std::string value = listbox.selectedValue(); + +// if(!Directory::isDir(dir->path() + dir->seperator())) +// { +// return; +// } + + listbox.clear(); + + INFO(filebrowser, "Changing path to '%s'\n", + (dir.path() + dir.seperator() + value).c_str()); + +#ifdef WIN32 + if(above_root && !value.empty()) + { + dir.setPath(value + dir.seperator()); + value.clear(); + above_root = false; + } +#endif + + if(value.empty() && !dir.isDir() && Directory::exists(dir.path())) + { + DEBUG(filebrowser, "Selecting file '%s'\n", dir.path().c_str()); + fileSelectNotifier(dir.path()); + return; + } + + if(!value.empty() && dir.fileExists(value)) + { + std::string file = dir.path() + dir.seperator() + value; + DEBUG(filebrowser, "Selecting file '%s'\n", file.c_str()); + fileSelectNotifier(file); + return; + } + + std::vector items; + +#ifdef WIN32 + if(Directory::isRoot(dir.path()) && (value == "..")) + { + DEBUG(filebrowser, "Showing partitions...\n"); + for(auto drive : dir.drives()) + { + ListBoxBasic::Item item; + item.name = drive.name; + item.value = drive.name; + items.push_back(item); + } + above_root = true; + } + else +#endif + { + if(!value.empty() && !dir.cd(value)) + { + DEBUG(filebrowser, "Error changing to '%s'\n", + (dir.path() + dir.seperator() + value).c_str()); + return; + } + + Directory::EntryList entries = dir.entryList(); + + if(entries.empty()) + { + dir.cdUp(); + entries = dir.entryList(); + } + + DEBUG(filebrowser, "Setting path of lineedit to %s\n", dir.path().c_str()); + lineedit.setText(dir.path()); + + for(auto entry : entries) + { + ListBoxBasic::Item item; + item.name = entry; + item.value = entry; + items.push_back(item); + } + } + + listbox.addItems(items); +} + +} // GUI:: -- cgit v1.2.3