r18882 - gnucash/trunk/src/gnc - Cutecash: Add Recent-File menu.

Christian Stimming cstim at code.gnucash.org
Tue Mar 9 16:53:43 EST 2010


Author: cstim
Date: 2010-03-09 16:53:43 -0500 (Tue, 09 Mar 2010)
New Revision: 18882
Trac: http://svn.gnucash.org/trac/changeset/18882

Added:
   gnucash/trunk/src/gnc/RecentFileMenu.cpp
   gnucash/trunk/src/gnc/RecentFileMenu.hpp
Modified:
   gnucash/trunk/src/gnc/CMakeLists.txt
   gnucash/trunk/src/gnc/mainwindow.cpp
   gnucash/trunk/src/gnc/mainwindow.hpp
   gnucash/trunk/src/gnc/mainwindow.ui
Log:
Cutecash: Add Recent-File menu.

Modified: gnucash/trunk/src/gnc/CMakeLists.txt
===================================================================
--- gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-09 19:38:41 UTC (rev 18881)
+++ gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-09 21:53:43 UTC (rev 18882)
@@ -13,6 +13,7 @@
   AccountItemModel.cpp
   Book.cpp
   Numeric.cpp
+  RecentFileMenu.cpp
   Session.cpp
   Split.cpp
   SplitListModel.cpp
@@ -22,6 +23,7 @@
 
 SET (gnc_QOBJECT_HEADERS
   AccountItemModel.hpp
+  RecentFileMenu.hpp
   SplitListModel.hpp
   mainwindow.hpp
 )

Added: gnucash/trunk/src/gnc/RecentFileMenu.cpp
===================================================================
--- gnucash/trunk/src/gnc/RecentFileMenu.cpp	                        (rev 0)
+++ gnucash/trunk/src/gnc/RecentFileMenu.cpp	2010-03-09 21:53:43 UTC (rev 18882)
@@ -0,0 +1,143 @@
+/*
+ * RecentFileMenu.hpp
+ * Copyright (C) 2010 Christian Stimming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+
+#include "RecentFileMenu.hpp"
+
+#include <QMenu>
+#include <QAction>
+#include <QSettings>
+
+namespace gnc
+{
+
+RecentFileMenu::RecentFileMenu(const QString& title, QWidget *parent)
+        : QMenu(title, parent)
+{
+    createActions();
+}
+
+
+RecentFileMenu::~RecentFileMenu()
+{
+}
+
+
+void RecentFileMenu::createActions()
+{
+    for (int i = 0; i < MaxRecentFiles; ++i)
+    {
+        QAction *newAct = new QAction(this);
+        newAct->setVisible(false);
+        connect(newAct, SIGNAL(triggered()),
+                this, SLOT(on_actionRecentFile()));
+        addAction(newAct);
+
+        m_actionRecentFile[i] = newAct;
+    }
+}
+
+
+void RecentFileMenu::usingFile(const QString& filename)
+{
+    if (filename.isEmpty())
+        return;
+
+    m_fileNames.removeAll(filename);
+    while (m_fileNames.size() > MaxRecentFiles - 1)
+        // remove last allowed position
+        m_fileNames.removeAt(MaxRecentFiles - 1);
+    m_fileNames.insert(0, filename);
+    updateMenu();
+}
+
+
+void RecentFileMenu::updateMenu()
+{
+    for (int i = 0; i < std::min(int(MaxRecentFiles), m_fileNames.size()); ++i)
+    {
+        const QString& qs = m_fileNames.at(i);
+        QAction *act = m_actionRecentFile[i];
+        act->setVisible(true);
+        act->setText(tr("&%1 %2").arg(i+1).arg(qs));
+        act->setData(qs);
+    }
+    for (int i = m_fileNames.size(); i < MaxRecentFiles; ++i)
+    {
+        QAction *act = m_actionRecentFile[i];
+        act->setVisible(false);
+    }
+
+    update();
+}
+
+
+void RecentFileMenu::readSettings(QSettings *settings, const QString &groupName)
+{
+    int size = settings->beginReadArray(groupName);
+    for (int i = 0; i < size; i++)
+    {
+        settings->setArrayIndex(i);
+        QString qs = settings->value("filename").toString();
+        if (!qs.isEmpty())
+        {
+            m_fileNames << qs;
+        }
+    }
+    settings->endArray();
+    updateMenu();
+}
+
+
+void RecentFileMenu::writeSettings(QSettings *settings, const QString &groupName)
+{
+    settings->remove(groupName);
+    settings->beginWriteArray(groupName);
+
+    int numElements = m_fileNames.size();
+    int j = 0;
+    for (int i = 0; i < numElements; i++)
+    {
+        QString qs = m_fileNames.at(i);
+        if (!qs.isEmpty())
+        {
+            settings->setArrayIndex(j++);
+            settings->setValue("filename", qs);
+        }
+    }
+
+    settings->endArray();
+}
+
+
+void RecentFileMenu::on_actionRecentFile()
+{
+    QAction *action = qobject_cast<QAction *>(sender());
+    if (action)
+    {
+        QString str = action->data().toString();
+        if (!str.isEmpty())
+            emit fileSelected(str);
+    }
+}
+
+} // END namespace gnc

Added: gnucash/trunk/src/gnc/RecentFileMenu.hpp
===================================================================
--- gnucash/trunk/src/gnc/RecentFileMenu.hpp	                        (rev 0)
+++ gnucash/trunk/src/gnc/RecentFileMenu.hpp	2010-03-09 21:53:43 UTC (rev 18882)
@@ -0,0 +1,92 @@
+/*
+ * RecentFileMenu.hpp
+ * Copyright (C) 2010 Christian Stimming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+#ifndef GNC_RECENTFILEMENU_HPP
+#define GNC_RECENTFILEMENU_HPP
+
+#include <QtGui/QMenu>
+#include <QtCore/QStringList>
+
+class QAction;
+class QSettings;
+
+namespace gnc
+{
+
+/**
+ * A menu that shows a list of recently opened files.
+ */
+class RecentFileMenu: public QMenu
+{
+    Q_OBJECT
+
+public:
+    RecentFileMenu(const QString& title, QWidget *parent = 0);
+    ~RecentFileMenu();
+
+    /**
+     * Read the internal list from a QSettings array.
+     * @param settings QSettings to read from
+     * @param groupName name of the array for QSettings::beginReadArray().
+     */
+    void readSettings(QSettings *settings, const QString &groupName);
+
+    /**
+     * Write the internal list to a QSettings array.
+     * @param settings QSettings to write to
+     * @param groupName name of the array for QSettings::beginWriteArray().
+     */
+    void writeSettings(QSettings *settings, const QString &groupName);
+
+public slots:
+    /**
+     * Record the given string as a filename that was (or is)
+     * being used in the application.  As a result the given
+     * filename will always become the new first menu entry.
+     */
+    void usingFile(const QString &fileName);
+
+signals:
+    /**
+     * This signal is emitted whenever the user selects a file from the internally managed
+     * menu (i.e. the user wants to open the given file).
+     */
+    void fileSelected(const QString &fileName);
+
+private slots:
+    void on_actionRecentFile();
+
+private:
+    void updateMenu();
+    void createActions();
+
+private:
+    QStringList m_fileNames;
+
+    enum { MaxRecentFiles = 5 };
+    QAction *m_actionRecentFile[MaxRecentFiles];
+
+};
+
+} // END namespace gnc
+
+#endif

Modified: gnucash/trunk/src/gnc/mainwindow.cpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-09 19:38:41 UTC (rev 18881)
+++ gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-09 21:53:43 UTC (rev 18882)
@@ -49,6 +49,7 @@
 #include "gnc/Numeric.hpp"
 #include "gnc/Split.hpp"
 #include "gnc/SplitListModel.hpp"
+#include "gnc/RecentFileMenu.hpp"
 
 namespace gnc
 {
@@ -105,6 +106,15 @@
     }
 }
 
+void MainWindow::loadFileQueried(const QString &fileName)
+{
+    if (maybeSave())
+    {
+        loadFile(fileName);
+    }
+}
+
+
 bool MainWindow::save()
 {
     if (curFile.isEmpty())
@@ -185,6 +195,11 @@
 
 void MainWindow::createToolBars()
 {
+    menuRecentFiles = new RecentFileMenu(tr("Open &Recent"));
+    ui->menuFile->insertMenu(ui->actionSave, menuRecentFiles);
+    connect(menuRecentFiles, SIGNAL(fileSelected(const QString &)),
+            this, SLOT(loadFileQueried(const QString&)));
+
     fileToolBar = addToolBar(tr("File"));
     fileToolBar->addAction(ui->actionNew);
     fileToolBar->addAction(ui->actionOpen);
@@ -206,6 +221,7 @@
     QSettings settings("Trolltech", "Application Example");
     QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
     QSize size = settings.value("size", QSize(400, 400)).toSize();
+    menuRecentFiles->readSettings(&settings, "RecentFiles");
     resize(size);
     move(pos);
 }
@@ -215,6 +231,7 @@
     QSettings settings("Trolltech", "Application Example");
     settings.setValue("pos", pos());
     settings.setValue("size", size());
+    menuRecentFiles->writeSettings(&settings, "RecentFiles");
 }
 
 bool MainWindow::maybeSave()
@@ -236,6 +253,7 @@
 
 void MainWindow::setCurrentFile(const QString &fileName)
 {
+    menuRecentFiles->usingFile(fileName);
     curFile = fileName;
 //     ui->textEdit->document()->setModified(false);
     setWindowModified(false);

Modified: gnucash/trunk/src/gnc/mainwindow.hpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-09 19:38:41 UTC (rev 18881)
+++ gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-09 21:53:43 UTC (rev 18882)
@@ -41,6 +41,8 @@
 namespace gnc
 {
 
+class RecentFileMenu;
+
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
@@ -52,6 +54,7 @@
 public slots:
     void anchorClicked(const QUrl &);
     void activatedAccount(const QModelIndex & index);
+    void loadFileQueried(const QString &fileName);
 
 protected:
     void closeEvent(QCloseEvent *event);
@@ -82,6 +85,7 @@
 
     QToolBar *fileToolBar;
     QToolBar *editToolBar;
+    RecentFileMenu *menuRecentFiles;
 
     Session m_session;
     AccountListModel *m_accountListModel;

Modified: gnucash/trunk/src/gnc/mainwindow.ui
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.ui	2010-03-09 19:38:41 UTC (rev 18881)
+++ gnucash/trunk/src/gnc/mainwindow.ui	2010-03-09 21:53:43 UTC (rev 18882)
@@ -145,7 +145,6 @@
     </property>
     <addaction name="actionNew"/>
     <addaction name="actionOpen"/>
-    <addaction name="actionRecent"/>
     <addaction name="actionSave"/>
     <addaction name="actionSave_as"/>
     <addaction name="separator"/>
@@ -389,14 +388,6 @@
     <string>&amp;Redo</string>
    </property>
   </action>
-  <action name="actionRecent">
-   <property name="text">
-    <string>Open &amp;Recent</string>
-   </property>
-   <property name="statusTip">
-    <string>Open an existing file from the list of recently opened files</string>
-   </property>
-  </action>
   <action name="actionViewAccountList">
    <property name="checkable">
     <bool>true</bool>



More information about the gnucash-changes mailing list