Extend menu to show the most recent files loaded by a main window application.

--HG--
branch : develop
This commit is contained in:
dismine 2014-02-10 21:31:22 +02:00
parent 396c9c1a8c
commit f88b7913f4
3 changed files with 139 additions and 43 deletions

View File

@ -79,24 +79,13 @@ MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent), ui(new Ui::MainWindow), pattern(0), doc(0), tool(Tool::ArrowTool), currentScene(0),
sceneDraw(0), sceneDetails(0), mouseCoordinate(0), helpLabel(0), view(0), isInitialized(false), dialogTable(0),
dialogTool(0), dialogHistory(0), comboBoxDraws(0), curFile(QString()), mode(Draw::Calculation),
currentDrawIndex(0), currentToolBoxIndex(0), drawMode(true)
currentDrawIndex(0), currentToolBoxIndex(0), drawMode(true), recentFileActs{0,0,0,0,0}, separatorAct(0)
{
ui->setupUi(this);
static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
{
//If there is no default working icon theme then we should
//use an icon theme that we provide via a .qrc file
//This case happens under Windows and Mac OS X
//This does not happen under GNOME or KDE
QIcon::setThemeName("win.icon.theme");
ui->actionNew->setIcon(QIcon::fromTheme("document-new"));
ui->actionOpen->setIcon(QIcon::fromTheme("document-open"));
ui->actionSave->setIcon(QIcon::fromTheme("document-save"));
ui->actionSaveAs->setIcon(QIcon::fromTheme("document-save-as"));
}
CreateActions();
CreateMenus();
ToolBarOption();
ToolBarDraws();
sceneDraw = new VMainGraphicsScene();
currentScene = sceneDraw;
connect(sceneDraw, &VMainGraphicsScene::mouseMove, this, &MainWindow::mouseMove);
@ -116,16 +105,6 @@ MainWindow::MainWindow(QWidget *parent)
helpLabel = new QLabel(tr("Create new pattern piece to start working."));
ui->statusBar->addWidget(helpLabel);
connect(ui->actionArrowTool, &QAction::triggered, this, &MainWindow::ActionAroowTool);
connect(ui->actionDraw, &QAction::triggered, this, &MainWindow::ActionDraw);
connect(ui->actionDetails, &QAction::triggered, this, &MainWindow::ActionDetails);
connect(ui->actionNewDraw, &QAction::triggered, this, &MainWindow::ActionNewDraw);
connect(ui->actionOptionDraw, &QAction::triggered, this, &MainWindow::OptionDraw);
connect(ui->actionSaveAs, &QAction::triggered, this, &MainWindow::SaveAs);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::Save);
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::Open);
connect(ui->actionNew, &QAction::triggered, this, &MainWindow::NewPattern);
connect(ui->actionTable, &QAction::triggered, this, &MainWindow::ActionTable);
connect(ui->toolButtonEndLine, &QToolButton::clicked, this, &MainWindow::ToolEndLine);
connect(ui->toolButtonLine, &QToolButton::clicked, this, &MainWindow::ToolLine);
connect(ui->toolButtonAlongLine, &QToolButton::clicked, this, &MainWindow::ToolAlongLine);
@ -158,10 +137,6 @@ MainWindow::MainWindow(QWidget *parent)
connect(timer, &QTimer::timeout, this, &MainWindow::AutoSavePattern);
timer->start(300000);
connect(ui->actionAbout_Qt, &QAction::triggered, this, &MainWindow::AboutQt);
connect(ui->actionAbout_Valentina, &QAction::triggered, this, &MainWindow::About);
connect(ui->actionExit, &QAction::triggered, this, &MainWindow::close);
ui->toolBox->setCurrentIndex(0);
ReadSettings();
@ -562,6 +537,15 @@ void MainWindow::tableClosed()
MinimumScrollBar();
}
void MainWindow::OpenRecentFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
{
LoadPattern(action->data().toString());
}
}
void MainWindow::showEvent( QShowEvent *event )
{
QMainWindow::showEvent( event );
@ -630,7 +614,6 @@ void MainWindow::ToolBarOption()
mouseCoordinate = new QLabel;
mouseCoordinate ->setText("0, 0");
ui->toolBarOption->addWidget(mouseCoordinate);
}
void MainWindow::ToolBarDraws()
@ -1268,6 +1251,21 @@ void MainWindow::setCurrentFile(const QString &fileName)
{
shownName = tr("untitled.val");
}
else
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(),
QApplication::applicationName());
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
files.prepend(fileName);
while (files.size() > MaxRecentFiles)
{
files.removeLast();
}
settings.setValue("recentFileList", files);
UpdateRecentFileActions();
}
shownName+="[*]";
setWindowTitle(shownName);
}
@ -1279,7 +1277,8 @@ QString MainWindow::strippedName(const QString &fullFileName)
void MainWindow::ReadSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "ValentinaTeam", "Valentina");
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(),
QApplication::applicationName());
QPoint pos = settings.value("pos", QPoint(10, 10)).toPoint();
QSize size = settings.value("size", QSize(1000, 800)).toSize();
resize(size);
@ -1288,7 +1287,8 @@ void MainWindow::ReadSettings()
void MainWindow::WriteSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "ValentinaTeam", "Valentina");
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(),
QApplication::applicationName());
settings.setValue("pos", pos());
settings.setValue("size", size());
}
@ -1309,6 +1309,83 @@ bool MainWindow::MaybeSave()
return true;
}
void MainWindow::UpdateRecentFileActions()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(),
QApplication::applicationName());
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), static_cast<int>(MaxRecentFiles));
for (int i = 0; i < numRecentFiles; ++i)
{
QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
recentFileActs[i]->setText(text);
recentFileActs[i]->setData(files[i]);
recentFileActs[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
{
recentFileActs[j]->setVisible(false);
}
separatorAct->setVisible(numRecentFiles > 0);
}
void MainWindow::CreateMenus()
{
for (int i = 0; i < MaxRecentFiles; ++i)
{
ui->menuFile->insertAction(ui->actionExit, recentFileActs[i]);
}
separatorAct = new QAction(this);
Q_CHECK_PTR(separatorAct);
separatorAct->setSeparator(true);
ui->menuFile->insertAction(ui->actionExit, separatorAct);
UpdateRecentFileActions();
}
void MainWindow::CreateActions()
{
ui->setupUi(this);
static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
{
//If there is no default working icon theme then we should
//use an icon theme that we provide via a .qrc file
//This case happens under Windows and Mac OS X
//This does not happen under GNOME or KDE
QIcon::setThemeName("win.icon.theme");
ui->actionNew->setIcon(QIcon::fromTheme("document-new"));
ui->actionOpen->setIcon(QIcon::fromTheme("document-open"));
ui->actionSave->setIcon(QIcon::fromTheme("document-save"));
ui->actionSaveAs->setIcon(QIcon::fromTheme("document-save-as"));
}
connect(ui->actionArrowTool, &QAction::triggered, this, &MainWindow::ActionAroowTool);
connect(ui->actionDraw, &QAction::triggered, this, &MainWindow::ActionDraw);
connect(ui->actionDetails, &QAction::triggered, this, &MainWindow::ActionDetails);
connect(ui->actionNewDraw, &QAction::triggered, this, &MainWindow::ActionNewDraw);
connect(ui->actionOptionDraw, &QAction::triggered, this, &MainWindow::OptionDraw);
connect(ui->actionSaveAs, &QAction::triggered, this, &MainWindow::SaveAs);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::Save);
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::Open);
connect(ui->actionNew, &QAction::triggered, this, &MainWindow::NewPattern);
connect(ui->actionTable, &QAction::triggered, this, &MainWindow::ActionTable);
connect(ui->actionAbout_Qt, &QAction::triggered, this, &MainWindow::AboutQt);
connect(ui->actionAbout_Valentina, &QAction::triggered, this, &MainWindow::About);
connect(ui->actionExit, &QAction::triggered, this, &MainWindow::close);
//Actions for recent files loaded by a main window application.
for (int i = 0; i < MaxRecentFiles; ++i)
{
recentFileActs[i] = new QAction(this);
Q_CHECK_PTR(recentFileActs[i]);
recentFileActs[i]->setVisible(false);
connect(recentFileActs[i], &QAction::triggered, this, &MainWindow::OpenRecentFile);
}
}
MainWindow::~MainWindow()
{
CancelTool();

View File

@ -357,6 +357,7 @@ public slots:
* @brief tableClosed handle after close layout window.
*/
void tableClosed();
void OpenRecentFile();
signals:
/**
* @brief ModelChosen emit after calculation all details.
@ -456,6 +457,9 @@ private:
* @brief drawMode true if we current draw scene.
*/
bool drawMode;
enum { MaxRecentFiles = 5 };
QAction *recentFileActs[MaxRecentFiles];
QAction *separatorAct;
/**
* @brief ToolBarOption enable option toolbar.
*/
@ -557,6 +561,9 @@ private:
* @return returns true in all cases, except when the user clicks Cancel.
*/
bool MaybeSave();
void UpdateRecentFileActions();
void CreateMenus();
void CreateActions();
};
#endif // MAINWINDOW_H

View File

@ -47,7 +47,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>144</width>
<width>150</width>
<height>150</height>
</rect>
</property>
@ -302,7 +302,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>150</width>
<height>58</height>
</rect>
</property>
@ -378,7 +378,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>150</width>
<height>104</height>
</rect>
</property>
@ -579,7 +579,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<width>150</width>
<height>58</height>
</rect>
</property>
@ -669,7 +669,7 @@
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
<string>&amp;File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
@ -680,14 +680,14 @@
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
<string>&amp;Help</string>
</property>
<addaction name="actionAbout_Qt"/>
<addaction name="actionAbout_Valentina"/>
</widget>
<widget class="QMenu" name="menuDrawing">
<property name="title">
<string>Pattern piece</string>
<string>&amp;Pattern piece</string>
</property>
<addaction name="actionNewDraw"/>
<addaction name="actionOptionDraw"/>
@ -760,6 +760,9 @@
<property name="text">
<string>New</string>
</property>
<property name="iconText">
<string>&amp;New</string>
</property>
<property name="toolTip">
<string>Create a new pattern</string>
</property>
@ -776,6 +779,9 @@
<property name="text">
<string>Open</string>
</property>
<property name="iconText">
<string>&amp;Open</string>
</property>
<property name="toolTip">
<string>Open file with pattern</string>
</property>
@ -792,6 +798,9 @@
<property name="text">
<string>Save</string>
</property>
<property name="iconText">
<string>&amp;Save</string>
</property>
<property name="toolTip">
<string>Save pattern</string>
</property>
@ -809,7 +818,7 @@
</iconset>
</property>
<property name="text">
<string>Save as</string>
<string>Save &amp;As...</string>
</property>
<property name="toolTip">
<string>Save not yet saved pattern</string>
@ -958,17 +967,20 @@
</action>
<action name="actionAbout_Qt">
<property name="text">
<string>About Qt</string>
<string>About &amp;Qt</string>
</property>
</action>
<action name="actionAbout_Valentina">
<property name="text">
<string>About Valentina</string>
<string>&amp;About Valentina</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
<string>E&amp;xit</string>
</property>
<property name="toolTip">
<string>Exit the application</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>