r18886 - gnucash/trunk/src/gnc - Cutecash: Enable closing and re-opening the different tab views.

Christian Stimming cstim at code.gnucash.org
Wed Mar 10 12:50:51 EST 2010


Author: cstim
Date: 2010-03-10 12:50:51 -0500 (Wed, 10 Mar 2010)
New Revision: 18886
Trac: http://svn.gnucash.org/trac/changeset/18886

Modified:
   gnucash/trunk/src/gnc/RecentFileMenu.cpp
   gnucash/trunk/src/gnc/mainwindow.cpp
   gnucash/trunk/src/gnc/mainwindow.hpp
   gnucash/trunk/src/gnc/mainwindow.ui
Log:
Cutecash: Enable closing and re-opening the different tab views.

Also, change many main window slots to make use of the auto-connection
feature because it makes the slot intention much easier to read.

Also, note how we store the Tab position, title, isEnabled state
in dynamically allocated properties in the Tab widget itself -
this is a rather cool feature of QObject here (see reallyRemoveTab()
and viewOrHideTab()).

Modified: gnucash/trunk/src/gnc/RecentFileMenu.cpp
===================================================================
--- gnucash/trunk/src/gnc/RecentFileMenu.cpp	2010-03-10 16:05:55 UTC (rev 18885)
+++ gnucash/trunk/src/gnc/RecentFileMenu.cpp	2010-03-10 17:50:51 UTC (rev 18886)
@@ -26,6 +26,7 @@
 #include <QMenu>
 #include <QAction>
 #include <QSettings>
+#include <QFileInfo>
 
 namespace gnc
 {
@@ -73,12 +74,14 @@
 
 void RecentFileMenu::updateMenu()
 {
+    setEnabled(!m_fileNames.isEmpty());
     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->setText(tr("&%1 %2").arg(i+1).arg(QFileInfo(qs).fileName()));
+        act->setStatusTip(qs);
         act->setData(qs);
     }
     for (int i = m_fileNames.size(); i < MaxRecentFiles; ++i)

Modified: gnucash/trunk/src/gnc/mainwindow.cpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-10 16:05:55 UTC (rev 18885)
+++ gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-10 17:50:51 UTC (rev 18886)
@@ -96,7 +96,8 @@
     }
 }
 
-void MainWindow::open()
+// Auto-connected to ui->actionOpen's signal triggered()
+void MainWindow::on_actionOpen_triggered()
 {
     if (maybeSave())
     {
@@ -106,7 +107,7 @@
     }
 }
 
-void MainWindow::loadFileQueried(const QString &fileName)
+void MainWindow::loadFileMaybe(const QString &fileName)
 {
     if (maybeSave())
     {
@@ -115,11 +116,12 @@
 }
 
 
-bool MainWindow::save()
+// Auto-connected to ui->actionSave's signal triggered()
+bool MainWindow::on_actionSave_triggered()
 {
     if (curFile.isEmpty())
     {
-        return saveAs();
+        return on_actionSave_as_triggered();
     }
     else
     {
@@ -127,7 +129,8 @@
     }
 }
 
-bool MainWindow::saveAs()
+// Auto-connected to ui->actionSave_as's signal triggered()
+bool MainWindow::on_actionSave_as_triggered()
 {
     QString fileName = QFileDialog::getSaveFileName(this);
     if (fileName.isEmpty())
@@ -136,7 +139,8 @@
     return saveFile(fileName);
 }
 
-void MainWindow::about()
+// Auto-connected to ui->actionAbout's signal triggered()
+void MainWindow::on_actionAbout_triggered()
 {
     QMessageBox::about(this, tr("About Application"),
                        tr("This is a Gnucash C++ gui example, from the Qt4 Application example. It demonstrates how to "
@@ -149,7 +153,8 @@
 //     setWindowModified(ui->textEdit->document()->isModified());
 }
 
-void MainWindow::anchorClicked(const QUrl &url)
+// Auto-connected to ui->textBrowser's signal anchorClicked()
+void MainWindow::on_textBrowser_anchorClicked(const QUrl &url)
 {
     QMessageBox::information(this, tr("Got you!"),
                              tr("Obviously you clicked the link with the URL %1.")
@@ -162,35 +167,29 @@
     ui->actionOpen->setShortcuts(QKeySequence::Open);
     ui->actionSave->setShortcuts(QKeySequence::Save);
     ui->actionSave_as->setShortcuts(QKeySequence::SaveAs);
+    ui->actionViewClose->setShortcuts(QKeySequence::Close);
 
     connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
-    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));
-
-    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
-    connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
     connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
 
 //     connect(ui->actionCut, SIGNAL(triggered()), ui->textEdit, SLOT(cut()));
 //     connect(ui->actionCopy, SIGNAL(triggered()), ui->textEdit, SLOT(copy()));
 //     connect(ui->actionPaste, SIGNAL(triggered()), ui->textEdit, SLOT(paste()));
 
-    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
     connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
     ui->actionCut->setEnabled(false);
     ui->actionCopy->setEnabled(false);
 
-    connect(ui->textBrowser, SIGNAL(anchorClicked(const QUrl &)),
-            this, SLOT(anchorClicked(const QUrl &)));
 //     connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
 //             ui->actionCut, SLOT(setEnabled(bool)));
 //     connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
 //             ui->actionCopy, SLOT(setEnabled(bool)));
 
     connect(ui->treeView, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(activatedAccount(const QModelIndex&)));
+            this, SLOT(accountItemActivated(const QModelIndex&)));
     connect(ui->tableView, SIGNAL(activated(const QModelIndex &)),
-            this, SLOT(activatedAccount(const QModelIndex&)));
+            this, SLOT(accountItemActivated(const QModelIndex&)));
 }
 
 void MainWindow::createToolBars()
@@ -198,7 +197,7 @@
     menuRecentFiles = new RecentFileMenu(tr("Open &Recent"));
     ui->menuFile->insertMenu(ui->actionSave, menuRecentFiles);
     connect(menuRecentFiles, SIGNAL(fileSelected(const QString &)),
-            this, SLOT(loadFileQueried(const QString&)));
+            this, SLOT(loadFileMaybe(const QString&)));
 
     fileToolBar = addToolBar(tr("File"));
     fileToolBar->addAction(ui->actionNew);
@@ -221,9 +220,9 @@
     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);
+    menuRecentFiles->readSettings(&settings, "RecentFiles");
 }
 
 void MainWindow::writeSettings()
@@ -244,7 +243,7 @@
                                       "Do you want to save your changes?"),
                                    QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
         if (ret == QMessageBox::Save)
-            return save();
+            return on_actionSave_triggered();
         else if (ret == QMessageBox::Cancel)
             return false;
     }
@@ -272,8 +271,98 @@
     return QFileInfo(fullFileName).fileName();
 }
 
-void MainWindow::activatedAccount(const QModelIndex & index)
+
+// ////////////////////////////////////////////////////////////
+
+// Auto-connected to ui->actionViewClose's signal triggered
+void MainWindow::on_actionViewClose_triggered()
 {
+    on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
+}
+
+// Auto-connected to ui->tabWidget's signal tabCloseRequested(int)
+void MainWindow::on_tabWidget_tabCloseRequested(int index)
+{
+    QWidget *widget = ui->tabWidget->widget(index);
+    if (widget == ui->textBrowserTab)
+    {
+        ui->actionViewWelcomepage->setChecked(false);
+        reallyRemoveTab(index);
+    }
+    else if (widget == ui->treeViewTab)
+    {
+        ui->actionViewAccountTree->setChecked(false);
+        reallyRemoveTab(index);
+    }
+    else if (widget == ui->tableViewTab)
+    {
+        ui->actionViewAccountList->setChecked(false);
+        reallyRemoveTab(index);
+    }
+    else
+    {
+        ui->tabWidget->removeTab(index);
+        delete widget;
+    }
+}
+
+// Auto-connected to ui->actionViewAccountTree's signal triggered()
+void MainWindow::on_actionViewAccountTree_triggered(bool checked)
+{
+    viewOrHideTab(checked, ui->treeViewTab);
+}
+
+// Auto-connected to ui->actionViewAccountList's signal triggered()
+void MainWindow::on_actionViewAccountList_triggered(bool checked)
+{
+    viewOrHideTab(checked, ui->tableViewTab);
+}
+
+// Auto-connected to ui->actionViewWelcomepage's signal triggered()
+void MainWindow::on_actionViewWelcomepage_triggered(bool checked)
+{
+    viewOrHideTab(checked, ui->textBrowserTab);
+}
+
+void MainWindow::viewOrHideTab(bool checked, QWidget *widget)
+{
+    if (checked)
+    {
+        QVariant tabLabel = widget->property("tab_label");
+        Q_ASSERT(tabLabel.isValid());
+        QVariant tabPosition = widget->property("tab_position");
+        Q_ASSERT(tabPosition.isValid());
+        QVariant tabIsCurrent = widget->property("tab_iscurrent");
+        Q_ASSERT(tabIsCurrent.isValid());
+        ui->tabWidget->insertTab(tabPosition.toInt(), widget, tabLabel.toString());
+        if (tabIsCurrent.toBool())
+            ui->tabWidget->setCurrentWidget(widget);
+    }
+    else
+    {
+        on_tabWidget_tabCloseRequested(ui->tabWidget->indexOf(widget));
+    }
+}
+
+void MainWindow::reallyRemoveTab(int index)
+{
+    QWidget *widget = ui->tabWidget->widget(index);
+    widget->setProperty("tab_label", ui->tabWidget->tabText(index));
+    widget->setProperty("tab_position", index);
+    widget->setProperty("tab_iscurrent", (index == ui->tabWidget->currentIndex()));
+    ui->tabWidget->removeTab(index);
+}
+
+// Auto-connected to ui->tabWidget's signal currentChanged(int)
+void MainWindow::on_tabWidget_currentChanged(int index)
+{
+    QWidget *widget = ui->tabWidget->widget(index);
+    bool tabWithAccounts = (widget != ui->textBrowserTab);
+    ui->menuAccount->setEnabled(tabWithAccounts);
+}
+
+void MainWindow::accountItemActivated(const QModelIndex & index)
+{
     if (index.model() != m_accountTreeModel
             && index.model() != m_accountListModel)
     {
@@ -292,7 +381,7 @@
 
     // We create a new model for this list of splits and also a view
     // widget for this list.
-    QTableView *tableView = new QTableView(this); // FIXME: Parent object unclear
+    QTableView *tableView = new QTableView(ui->tabWidget); // FIXME: Is this parent correct?
     SplitListModel *smodel = new SplitListModel(Split::fromGList(slist), tableView);
     tableView->setModel(smodel);
     tableView->setAlternatingRowColors(true);
@@ -300,8 +389,6 @@
     // Insert this as a new tab
     ui->tabWidget->addTab(tableView, account.getName());
     ui->tabWidget->setCurrentWidget(tableView);
-
-    // Right now it cannot be deleted - this will be implemented later.
 }
 
 // ////////////////////////////////////////////////////////////
@@ -361,8 +448,6 @@
 
         gnc_hook_run(HOOK_NEW_BOOK, NULL);
 
-        //gnc_gui_refresh_all ();
-
         /* Call this after re-enabling events. */
         gnc_book_opened (m_session);
 
@@ -372,7 +457,7 @@
 
 namespace
 {
-/** This is a workaround functor so that we can obtain a
+/** This is a workaround function object so that we can obtain a
  * QofPercentageFunc without extra boost::bind usage; obviously due to
  * the static member variable it will not work if multiple instances
  * are in use simultaneously */

Modified: gnucash/trunk/src/gnc/mainwindow.hpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-10 16:05:55 UTC (rev 18885)
+++ gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-10 17:50:51 UTC (rev 18886)
@@ -52,19 +52,25 @@
     ~MainWindow();
 
 public slots:
-    void anchorClicked(const QUrl &);
-    void activatedAccount(const QModelIndex & index);
-    void loadFileQueried(const QString &fileName);
+    void accountItemActivated(const QModelIndex & index);
+    void loadFileMaybe(const QString &fileName);
 
 protected:
     void closeEvent(QCloseEvent *event);
 
 private slots:
     void newFile();
-    void open();
-    bool save();
-    bool saveAs();
-    void about();
+    void on_actionOpen_triggered();
+    bool on_actionSave_triggered();
+    void on_actionAbout_triggered();
+    bool on_actionSave_as_triggered();
+    void on_tabWidget_tabCloseRequested(int index);
+    void on_tabWidget_currentChanged(int index);
+    void on_textBrowser_anchorClicked(const QUrl &);
+    void on_actionViewAccountTree_triggered(bool checked);
+    void on_actionViewAccountList_triggered(bool checked);
+    void on_actionViewWelcomepage_triggered(bool checked);
+    void on_actionViewClose_triggered();
     void documentWasModified();
 
 private:
@@ -78,6 +84,8 @@
     bool saveFile(const QString &fileName);
     void setCurrentFile(const QString &fileName);
     QString strippedName(const QString &fullFileName);
+    void viewOrHideTab(bool checkedView, QWidget *widget);
+    void reallyRemoveTab(int index);
 
     Ui::MainWindow *ui;
 

Modified: gnucash/trunk/src/gnc/mainwindow.ui
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.ui	2010-03-10 16:05:55 UTC (rev 18885)
+++ gnucash/trunk/src/gnc/mainwindow.ui	2010-03-10 17:50:51 UTC (rev 18886)
@@ -161,6 +161,9 @@
     <property name="title">
      <string>&amp;Account</string>
     </property>
+    <property name="enabled">
+     <bool>false</bool>
+    </property>
     <addaction name="actionOpenAccount"/>
     <addaction name="actionNewAccount"/>
     <addaction name="actionDeleteAccount"/>
@@ -182,10 +185,11 @@
     <property name="title">
      <string>&amp;View</string>
     </property>
+    <addaction name="actionViewClose"/>
+    <addaction name="separator"/>
+    <addaction name="actionViewWelcomepage"/>
     <addaction name="actionViewAccountList"/>
     <addaction name="actionViewAccountTree"/>
-    <addaction name="separator"/>
-    <addaction name="action_Close_View"/>
    </widget>
    <addaction name="menuFile"/>
    <addaction name="menuEdit"/>
@@ -388,6 +392,17 @@
     <string>&amp;Redo</string>
    </property>
   </action>
+  <action name="actionViewWelcomepage">
+   <property name="checkable">
+    <bool>true</bool>
+   </property>
+   <property name="checked">
+    <bool>true</bool>
+   </property>
+   <property name="text">
+    <string>&amp;Welcome Page</string>
+   </property>
+  </action>
   <action name="actionViewAccountList">
    <property name="checkable">
     <bool>true</bool>
@@ -410,9 +425,9 @@
     <string>Account &amp;Tree</string>
    </property>
   </action>
-  <action name="action_Close_View">
+  <action name="actionViewClose">
    <property name="text">
-    <string>&amp;Close View</string>
+    <string>&amp;Close Current Tab</string>
    </property>
   </action>
  </widget>
@@ -437,37 +452,5 @@
     </hint>
    </hints>
   </connection>
-  <connection>
-   <sender>actionViewAccountList</sender>
-   <signal>toggled(bool)</signal>
-   <receiver>tableView</receiver>
-   <slot>setVisible(bool)</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>-1</x>
-     <y>-1</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>352</x>
-     <y>343</y>
-    </hint>
-   </hints>
-  </connection>
-  <connection>
-   <sender>actionViewAccountTree</sender>
-   <signal>toggled(bool)</signal>
-   <receiver>treeView</receiver>
-   <slot>setVisible(bool)</slot>
-   <hints>
-    <hint type="sourcelabel">
-     <x>-1</x>
-     <y>-1</y>
-    </hint>
-    <hint type="destinationlabel">
-     <x>352</x>
-     <y>343</y>
-    </hint>
-   </hints>
-  </connection>
  </connections>
 </ui>



More information about the gnucash-changes mailing list