~mrp/aerodramus

d08883b232bc9f935692ce9af890b77a5a256f1f — Mark Penner 6 months ago 1ec09b7
add closeCurrentPage action

the list item should be removed before the page so the indexes do not
get out of sync. Since QListWidget::currentRowChanged is connected to
QStackedWidget::setCurrentIndex, when the list item gets removed, the
index for both list and stack get changed from the index that will get
removed. Then when the page gets removed the stack index does not need
to change.
3 files changed, 21 insertions(+), 0 deletions(-)

M src/MainWindow.cpp
M src/MainWindow.h
M src/aerodramusui.rc
M src/MainWindow.cpp => src/MainWindow.cpp +15 -0
@@ 34,6 34,12 @@ void MainWindow::setupActions()
{
    KStandardAction::open(this, &MainWindow::openFile, actionCollection());
    KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());

    QAction *closeCurrentPageAction = new QAction(this);
    closeCurrentPageAction->setText(i18n("&Close Page"));
    closeCurrentPageAction->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));
    actionCollection()->addAction(QStringLiteral("closeCurrentPage"), closeCurrentPageAction);
    connect(closeCurrentPageAction, &QAction::triggered, this, &MainWindow::closeCurrentPage);
}

void MainWindow::openFile()


@@ 51,3 57,12 @@ void MainWindow::addPage(const QString &title, QWidget *widget)
    m_pageList->addItem(title);
    m_pageList->setCurrentRow(m_pageList->count() - 1, QItemSelectionModel::ClearAndSelect);
}

void MainWindow::closeCurrentPage()
{
    QWidget *page = m_pageStack->currentWidget();
    int row = m_pageList->currentIndex().row();
    delete m_pageList->takeItem(row);
    m_pageStack->removeWidget(page);
    delete page;
}

M src/MainWindow.h => src/MainWindow.h +1 -0
@@ 24,6 24,7 @@ private:
private Q_SLOTS:
    void openFile();
    void addPage(const QString &title, QWidget *widget);
    void closeCurrentPage();
};

#endif // MAINWINDOW_H

M src/aerodramusui.rc => src/aerodramusui.rc +5 -0
@@ 6,8 6,13 @@
     xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd">
  <MenuBar>
    <Menu name="view" >
      <Action name="closeCurrentPage" />
    </Menu>
  </MenuBar>

  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="closeCurrentPage" />
  </ToolBar>
</gui>