diff --git a/qad/application/emainwindow.cpp b/qad/application/emainwindow.cpp index 7c80935..79dd409 100644 --- a/qad/application/emainwindow.cpp +++ b/qad/application/emainwindow.cpp @@ -19,11 +19,16 @@ #include "emainwindow.h" #include #include +#include #include EMainWindow::EMainWindow(QWidget * parent): QMainWindow(parent), action_show_all_tools(this), action_hide_all_tools(this), action_show_all_docks(this), action_hide_all_docks(this), first_show(true) { + tid = 0; + menu_recent = 0; + action_clear_recent = new QAction(QIcon(":/icons/edit-clear.png"), tr("Clear recent list"), this); + connect(action_clear_recent, SIGNAL(triggered()), this, SLOT(clearRecent())); qRegisterMetaType("Qt::DockWidgetArea"); action_show_all_tools.setText(trUtf8("Show all")); action_show_all_docks.setText(trUtf8("Show all")); @@ -33,6 +38,7 @@ action_show_all_docks(this), action_hide_all_docks(this), first_show(true) { action_show_all_docks.setIcon(QIcon(":/icons/layer-visible-on.png")); action_hide_all_tools.setIcon(QIcon(":/icons/layer-visible-off.png")); action_hide_all_docks.setIcon(QIcon(":/icons/layer-visible-off.png")); + max_recent = 8; setChanged(false); initMenus(); initSession(); @@ -41,10 +47,33 @@ action_show_all_docks(this), action_hide_all_docks(this), first_show(true) { EMainWindow::~EMainWindow() { + if (tid > 0) killTimer(tid); + tid = 0; saveSession(); } +void EMainWindow::setRecentFiles(const QStringList & rl) { + clearRecent(); + for (int i = rl.size() - 1; i >= 0; --i) + addToRecent(rl[i]); +} + + +QStringList EMainWindow::recentFiles() const { + QStringList ret; + foreach (QAction * a, actions_recent) + ret << a->data().toString(); + return ret; +} + + +void EMainWindow::setRecentMenu(QMenu * m) { + menu_recent = m; + prepareRecent(); +} + + void EMainWindow::showEvent(QShowEvent * e) { QWidget::showEvent(e); initMenus(); @@ -169,6 +198,42 @@ QMenu * EMainWindow::createPopupMenu() { } +void EMainWindow::addToRecent(const QString & path) { + if (path.isEmpty()) return; + QFileInfo fi(path); + QString fp = fi.absoluteFilePath(); + bool insert = true; + for (int i = 0; i < actions_recent.size(); ++i) + if (actions_recent[i]->data().toString() == fp) { + actions_recent.push_front(actions_recent.takeAt(i)); + insert = false; + prepareRecent(); + break; + } + if (!insert) return; + QAction * a = new QAction(this); + a->setData(fp); + connect(a, SIGNAL(triggered()), this, SLOT(recentTriggered())); + actions_recent.push_front(a); + while (actions_recent.size() > max_recent) + delete actions_recent.takeLast(); + prepareRecent(); +} + + +void EMainWindow::prepareRecent() { + for (int i = 0; i < actions_recent.size(); ++i) { + QAction * a = actions_recent[i]; + a->setText(QString("&%1 - %2").arg(i + 1).arg(a->data().toString())); + } + if (!menu_recent) return; + menu_recent->clear(); + menu_recent->addActions(actions_recent); + menu_recent->addSeparator(); + menu_recent->addAction(action_clear_recent); +} + + void EMainWindow::initMenus() { action_show_all_tools.disconnect(); action_hide_all_tools.disconnect(); @@ -298,6 +363,24 @@ void EMainWindow::closeDock(int index) { } +void EMainWindow::recentTriggered() { + QAction * a = qobject_cast(sender()); + if (!a) return; + QString path = a->data().toString(); + if (path.isEmpty()) return; + if (!checkSave()) return; + if (load(path)) + addToRecent(path); +} + + +void EMainWindow::setMaxRecentItems(int mr) { + max_recent = qMax(0, mr); + for (int i = actions_recent.size() - 1; i >= mr; --i) + delete actions_recent.takeLast(); +} + + void EMainWindow::newFile() { if (!checkSave()) return; reset(true); @@ -309,7 +392,8 @@ void EMainWindow::openFile() { if (!checkSave()) return; QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select file to open"), file_name, loadFilter()); if (ret.isEmpty()) return; - load(ret); + if (load(ret)) + addToRecent(ret); } @@ -321,7 +405,8 @@ bool EMainWindow::saveFile(bool ask) { return true; } if (file_name.isEmpty()) return saveAsFile(); - save(file_name); + if (save(file_name)) + addToRecent(file_name); return true; } @@ -329,6 +414,14 @@ bool EMainWindow::saveFile(bool ask) { bool EMainWindow::saveAsFile() { QString ret = QFileDialog::getSaveFileName(this, trUtf8("Select file to save"), file_name, saveFilter()); if (ret.isEmpty()) return false; - save(ret); + if (save(ret)) + addToRecent(ret); return true; } + + +void EMainWindow::clearRecent() { + qDeleteAll(actions_recent); + actions_recent.clear(); + prepareRecent(); +} diff --git a/qad/application/emainwindow.h b/qad/application/emainwindow.h index e7bd332..ae0615a 100644 --- a/qad/application/emainwindow.h +++ b/qad/application/emainwindow.h @@ -50,6 +50,7 @@ signals: class EMainWindow: public QMainWindow { Q_OBJECT + Q_PROPERTY(int maxRecentItems READ maxRecentItems WRITE setMaxRecentItems) public: EMainWindow(QWidget * parent = 0); ~EMainWindow(); @@ -59,6 +60,11 @@ public: virtual bool save(const QString & path) {return true;} void addSeparator() {} + void setRecentFiles(const QStringList & rl); + QStringList recentFiles() const; + void setRecentMenu(QMenu * m); + + int maxRecentItems() const {return max_recent;} protected: // Qt`s overloaded @@ -67,6 +73,8 @@ protected: bool eventFilter(QObject * o, QEvent * e); void timerEvent(QTimerEvent * e); QMenu * createPopupMenu(); + void addToRecent(const QString & path); + void prepareRecent(); void init(const QString & config) {session.setFile(config); initMenus(); initSession(); loadSession();} // unusable void saveSession(); @@ -87,9 +95,12 @@ protected: QString file_name; QList tbars; QList tdocks; + QList actions_recent; + QAction * action_clear_recent; + QMenu * menu_recent; SessionManager session; bool isChanged, first_show; - int tid; + int tid, max_recent; private slots: void changedDock(); @@ -97,13 +108,16 @@ private slots: void sessionSaving(QPIConfig & conf) {savingSession(conf);} // void changedDockClose(QObject * dock); void closeDock(int index); + void recentTriggered(); public slots: + void setMaxRecentItems(int mr); void changed() {setChanged(true);} void newFile(); void openFile(); bool saveFile(bool ask = false); bool saveAsFile(); + void clearRecent(); signals: diff --git a/qad/application/qad_application.qrc b/qad/application/qad_application.qrc index a981101..6a6c8f9 100644 --- a/qad/application/qad_application.qrc +++ b/qad/application/qad_application.qrc @@ -10,6 +10,7 @@ ../icons/document-save-all.png ../icons/document-save-as.png ../icons/document-open.png + ../icons/document-open-recent.png ../icons/document-close.png ../icons/edit-clear.png ../icons/edit-clear-locationbar-rtl.png diff --git a/qad/icons/document-open-recent.png b/qad/icons/document-open-recent.png new file mode 100644 index 0000000..5dea408 Binary files /dev/null and b/qad/icons/document-open-recent.png differ