From cd9bfc3f662be661687868203dc38a1b579dac47 Mon Sep 17 00:00:00 2001 From: hama Date: Wed, 6 May 2015 23:30:57 +0200 Subject: [PATCH] context menu works --- appl/refind/dialogoptions.cpp | 25 ++++++++++++++- appl/refind/dialogoptions.hpp | 1 + appl/refind/dialogoptions.ui | 2 +- appl/refind/mainwindow.cpp | 13 -------- appl/refind/utils.cpp | 58 ++++++++++++++++++++++++++++++----- base/ReQStringUtil.cpp | 26 ++++++++++++++++ base/ReQStringUtil.hpp | 1 + 7 files changed, 104 insertions(+), 22 deletions(-) diff --git a/appl/refind/dialogoptions.cpp b/appl/refind/dialogoptions.cpp index 8a8c131..82eebbd 100644 --- a/appl/refind/dialogoptions.cpp +++ b/appl/refind/dialogoptions.cpp @@ -9,7 +9,15 @@ #include "base/rebase.hpp" #include "dialogoptions.hpp" #include "ui_dialogoptions.h" +#include "math.h" +#include +/** + * Constructor. + * + * @param handlers the current list of the context menu handlers + * @param parent the widget parent + */ DialogOptions::DialogOptions(ContextHandlerList& handlers, QWidget *parent) : QDialog(parent), ui(new Ui::DialogOptions), @@ -20,8 +28,11 @@ DialogOptions::DialogOptions(ContextHandlerList& handlers, QWidget *parent) : connect(ui->pushButtonAdd, SIGNAL(clicked()), this, SLOT(add())); connect(ui->pushButtonDel, SIGNAL(clicked()), this, SLOT(del())); connect(ui->pushButtonDown, SIGNAL(clicked()), this, SLOT(down())); + connect(ui->pushButtonSelectProgram, SIGNAL(clicked()), this, + SLOT(selectProgram())); connect(ui->pushButtonUp, SIGNAL(clicked()), this, SLOT(up())); connect(ui->tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(cellEntered(int,int))); + connect(ui->tableWidget, SIGNAL(cellActivated(int,int)), this, SLOT(cellEntered(int,int))); for (int ix = 0; ix < ui->comboBoxFileType->count(); ix++) m_fileTypes.append(ui->comboBoxFileType->itemText(ix)); for (int ix = 0; ix < ui->comboBoxDirMode->count(); ix++) @@ -107,7 +118,10 @@ void DialogOptions::currentToTable(int row){ */ void DialogOptions::del(){ int count = ui->tableWidget->rowCount() - 1; - for (int row = m_selectedRow; row < count; row++){ + int start = m_selectedRow; + if (start == 0) + start = 1; + for (int row = start; row < count; row++){ swapRows(row, row - 1); } ui->tableWidget->setRowCount(count); @@ -207,6 +221,15 @@ void DialogOptions::swapRows(int row1, int row2){ } } +/** + * Handles the event "pushed button select program file". + */ +void DialogOptions::selectProgram(){ + QString file = QFileDialog::getOpenFileName(NULL, tr("Select Program File"), + ui->comboBoxProgram->currentText()); + if (!file.isEmpty()) + ui->comboBoxProgram->setCurrentText(file); +} /** * Handles the event "pushed button up". */ diff --git a/appl/refind/dialogoptions.hpp b/appl/refind/dialogoptions.hpp index 3601087..4e978b8 100644 --- a/appl/refind/dialogoptions.hpp +++ b/appl/refind/dialogoptions.hpp @@ -26,6 +26,7 @@ public: void cellEntered(int row, int column); void del(); void down(); + void selectProgram(); void up(); private: void currentToTable(int row); diff --git a/appl/refind/dialogoptions.ui b/appl/refind/dialogoptions.ui index 7d486d4..03fa9a7 100644 --- a/appl/refind/dialogoptions.ui +++ b/appl/refind/dialogoptions.ui @@ -85,7 +85,7 @@ - Current Directory + Directory Mode diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index 33fb4a8..8aa58e1 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -499,19 +499,6 @@ void MainWindow::prepareContextMenu(){ SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(handleTableContextMenu(const QPoint&))); ContextHandler* handler = new ContextHandler(); - handler->m_text = "kedit"; - handler->m_program = "/usr/bin/kate"; - handler->m_arguments = "${full}"; - handler->m_directoryMode = ContextHandler::DM_UNDEF; - handler->m_fileType = ContextHandler::FT_FILE; - m_contextHandlers.list().append(handler); - handler = new ContextHandler(); - handler->m_text = "bash"; - handler->m_program = "/usr/bin/konsole"; - handler->m_arguments = "${full}"; - handler->m_directoryMode = ContextHandler::DM_TO_FILE; - handler->m_fileType = ContextHandler::FT_DIR; - m_contextHandlers.list().append(handler); } /** diff --git a/appl/refind/utils.cpp b/appl/refind/utils.cpp index d5123a9..71a48ce 100644 --- a/appl/refind/utils.cpp +++ b/appl/refind/utils.cpp @@ -93,12 +93,29 @@ void ContextHandlerList::save(ReStateStorage& storage){ for (int ix = 0; ix < m_list.size(); ix++){ ContextHandler* handler = m_list.at(ix); value = handler->m_text + "\t" + handler->m_program + "\t" + + handler->m_arguments + "\t" + QString::number(handler->m_fileType) + "\t" + QString::number(handler->m_directoryMode); storage.store(name, value, ix); } } +ContextHandler* createHandlerIfExists(const QString& file, + ContextHandler::FileType fileType = ContextHandler::FT_FILE){ + QFileInfo info(file); + ContextHandler* handler = NULL; + if (info.exists()){ + handler = new ContextHandler; + handler->m_text = ReQStringUtil::nodeOf(file); + handler->m_program = file; + handler->m_arguments = fileType; + handler->m_directoryMode = fileType == ContextHandler::FT_FILE + ? ContextHandler::DM_TO_PARENT + : ContextHandler::DM_TO_FILE; + } + return handler; +} + /** * Reads the list from the storage. * @@ -110,24 +127,51 @@ void ContextHandlerList::restore(ReStateStorage& storage){ int ix = -1; QString value; QStringList cols; + ContextHandler* handler; while (true){ ix++; value = storage.restore(name, ix); if (value.isEmpty()) break; cols = value.split('\t'); - if (cols.size() != 5) - break; - ContextHandler* handler = new ContextHandler; + if (cols.size() < 3) + continue; + handler = new ContextHandler; handler->m_text = cols.at(0); handler->m_program = cols.at(1); handler->m_arguments = cols.at(2); - handler->m_fileType = ContextHandler::FileType( - 1 + atol(cols.at(3).toUtf8().constData())); - handler->m_directoryMode = ContextHandler::DirMode( - 1 + atol(cols.at(4).toUtf8().constData())); + const char* sValue = cols.at(3).toUtf8().constData(); + handler->m_fileType = ContextHandler::FileType(atol(sValue)); + sValue = cols.at(4).toUtf8().constData(); + handler->m_directoryMode = ContextHandler::DirMode(atol(sValue)); m_list.append(handler); } + if (m_list.size() == 0){ +#if defined __linux__ + handler = createHandlerIfExists("/usr/bin/kate"); + if (handler == NULL) + handler = createHandlerIfExists("/usr/bin/geany"); + if (handler == NULL) + handler = createHandlerIfExists("/usr/bin/kwrite"); + if (handler == NULL) + handler = createHandlerIfExists("/usr/bin/gedit"); + if (handler != NULL) + m_list.append(handler); + handler = createHandlerIfExists("/usr/bin/konsole", ContextHandler::FT_DIR); + if (handler == NULL) + handler = createHandlerIfExists("/usr/bin/gnome-terminal", ContextHandler::FT_DIR); + if (handler != NULL) + m_list.append(handler); +#elif defined __WIN32__ + handler = createHandlerIfExists("c:\\windows\\system32\\notepad.exe"); + if (handler == NULL) + m_contextHandlers.list().append(handler); + handler = createHandlerIfExists("c:\\windows\\system32\\cmd.exe", ContextHandler::FT_DIR); + if (handler == NULL) + m_list.append(handler); + +#endif + } } /** diff --git a/base/ReQStringUtil.cpp b/base/ReQStringUtil.cpp index 0b1b8eb..94c3766 100644 --- a/base/ReQStringUtil.cpp +++ b/base/ReQStringUtil.cpp @@ -355,6 +355,32 @@ int ReQStringUtil::lengthOfReal(const ReString& text, int start, qreal* pValue){ return found ? ix - start : 0; } +/** + * Extracts the node of a filename. + * + * The node is the filename without path. + * + * @param filename the filename (with or without path) + * @return the node of filename + */ +ReString ReQStringUtil::nodeOf(const ReString& filename){ + QString rc; + + int index = filename.lastIndexOf('/'); + if (index >= 0) + rc = filename.mid(index + 1); + else{ +#if defined __WIN32__ + index = filename.lastIndexOf('\\'); + if (index < 0) + rc = filename; + else + rc = filename.mid(index + 1); +#endif + } + return rc; +} + /** * Appends a relative path to base directory name (absolute or relative). * diff --git a/base/ReQStringUtil.hpp b/base/ReQStringUtil.hpp index 1846f13..950163f 100644 --- a/base/ReQStringUtil.hpp +++ b/base/ReQStringUtil.hpp @@ -42,6 +42,7 @@ public: return path; #endif } + static ReString nodeOf(const ReString& filename); static QString pathAppend(const QString& base, const QString& path); static bool replacePlaceholders(QString& text, const QMap & placeholders, QString* error); -- 2.39.5