Cutecash: Add Recent-File menu.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18882 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming
2010-03-09 21:53:43 +00:00
parent 994c6dcda7
commit cec36eea50
6 changed files with 259 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ SET (gnc_SOURCES
AccountItemModel.cpp
Book.cpp
Numeric.cpp
RecentFileMenu.cpp
Session.cpp
Split.cpp
SplitListModel.cpp
@@ -22,6 +23,7 @@ SET (gnc_SOURCES
SET (gnc_QOBJECT_HEADERS
AccountItemModel.hpp
RecentFileMenu.hpp
SplitListModel.hpp
mainwindow.hpp
)

143
src/gnc/RecentFileMenu.cpp Normal file
View File

@@ -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@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

View File

@@ -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@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

View File

@@ -49,6 +49,7 @@ extern "C"
#include "gnc/Numeric.hpp"
#include "gnc/Split.hpp"
#include "gnc/SplitListModel.hpp"
#include "gnc/RecentFileMenu.hpp"
namespace gnc
{
@@ -105,6 +106,15 @@ void MainWindow::open()
}
}
void MainWindow::loadFileQueried(const QString &fileName)
{
if (maybeSave())
{
loadFile(fileName);
}
}
bool MainWindow::save()
{
if (curFile.isEmpty())
@@ -185,6 +195,11 @@ void MainWindow::createActions()
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 @@ void MainWindow::readSettings()
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 @@ void MainWindow::writeSettings()
QSettings settings("Trolltech", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());
menuRecentFiles->writeSettings(&settings, "RecentFiles");
}
bool MainWindow::maybeSave()
@@ -236,6 +253,7 @@ bool MainWindow::maybeSave()
void MainWindow::setCurrentFile(const QString &fileName)
{
menuRecentFiles->usingFile(fileName);
curFile = fileName;
// ui->textEdit->document()->setModified(false);
setWindowModified(false);

View File

@@ -41,6 +41,8 @@ class MainWindow;
namespace gnc
{
class RecentFileMenu;
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -52,6 +54,7 @@ public:
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 @@ private:
QToolBar *fileToolBar;
QToolBar *editToolBar;
RecentFileMenu *menuRecentFiles;
Session m_session;
AccountListModel *m_accountListModel;

View File

@@ -145,7 +145,6 @@ p, li { white-space: pre-wrap; }
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionRecent"/>
<addaction name="actionSave"/>
<addaction name="actionSave_as"/>
<addaction name="separator"/>
@@ -389,14 +388,6 @@ p, li { white-space: pre-wrap; }
<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>