Skip to content

Commit deafa9e

Browse files
committed
qmdiSplitTab: properly support "new"
When clicking ghenew button from the menu, previously 2 new files were added. The reason was that I hacked inside the split tab to accept the new file command. The correct solution is to leave it to the default `addTab()` method. However, this broke another thing (which I tried to avoid in the previous hack): if you click new in a split which was not active, I was expecting that split to add a new file. Instead - a new tab was added inside the active tab. Solution: inside the click handler of the tool buttons, make that tab active. Now everything works as expected. This is the code in qmdiSplitTab. Code in SplitTabsPlugin was removed, as it was wrong to begin with. All other code is random cleanups.
1 parent 269192d commit deafa9e

5 files changed

Lines changed: 16 additions & 23 deletions

File tree

src/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ int main(int argc, char *argv[]) {
9090
pluginManager.restoreSettings();
9191
pluginManager.show();
9292

93-
#if !defined(USE_SPLIT)
9493
pluginManager.connect(&pluginManager, &PluginManager::newFileRequested,
9594
[textEditorPlugin]() { textEditorPlugin->fileNew(); });
96-
#endif
9795

9896
pluginManager.openFiles(parser.positionalArguments());
9997
return app.exec();

src/plugins/SplitTabsPlugin/SplitTabsPlugin.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,6 @@ void SplitTabsPlugin::on_client_merged(qmdiHost *host) {
108108
manager->replaceMdiServer(split);
109109
manager->addBuiltinActions();
110110
manager->updateGUI();
111-
112-
connect(manager, &PluginManager::newFileRequested, [this](QObject *s) {
113-
auto manager = getManager();
114-
auto tab = qobject_cast<QTabWidget *>(s->parent());
115-
auto index = split->findSplitIndex(tab);
116-
if (!tab || index < 0) {
117-
// textEditorPlugin->fileNew();
118-
return;
119-
}
120-
121-
// we know where exactly to put this
122-
auto client = textEditorPlugin->fileNewEditor();
123-
auto editor = dynamic_cast<QWidget *>(client);
124-
split->addTabToSplit(index, editor, client->mdiClientName, client->mdiClientFileName());
125-
client->mdiServer = manager->getMdiServer();
126-
editor->setFocus();
127-
});
128111
}
129112

130113
void SplitTabsPlugin::on_client_unmerged(qmdiHost *) {}

src/widgets/SplitTabWidget.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ SplitTabWidget::SplitTabWidget(QWidget *parent)
324324
void SplitTabWidget::addTab(QWidget *widget, const QString &label, const QString &tooltip) {
325325
addTabToCurrentSplit(widget, label, tooltip);
326326

327+
// This code deals with pre-loading. You can configure a the pre-state of the split tab
328+
// and then when adding new tabs, the widget will split as requested.
327329
if (!savedSplitCount.empty()) {
328330
auto splitsCount = splitter->count();
329331
auto currentTabIndex = findSplitIndex(currentTabWidget);
@@ -468,6 +470,12 @@ void SplitTabWidget::updateCurrentTabWidget(QTabWidget *newCurrent) {
468470
if (currentTabWidget == newCurrent) {
469471
return;
470472
}
473+
if (newCurrent->parentWidget() != splitter) {
474+
qDebug()
475+
<< "updateCurrentTabWidget passing wrong parent, will not update current tab, parent="
476+
<< newCurrent->parentWidget();
477+
return;
478+
}
471479
currentTabWidget = newCurrent;
472480
if (currentTabWidget && currentTabWidget->currentWidget()) {
473481
onTabFocusChanged(currentTabWidget->currentWidget(), true);

src/widgets/SplitTabWidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class SplitTabWidget : public QWidget {
120120

121121
bool closeSplitWhenEmpty = true;
122122

123+
void updateCurrentTabWidget(QTabWidget *newCurrent);
124+
123125
signals:
124126
void emptyAreaDoubleClicked(QTabWidget *tabWidget, const QPoint &pos);
125127

@@ -129,7 +131,6 @@ class SplitTabWidget : public QWidget {
129131
SplitterWithWidgetAdded *splitter = nullptr;
130132
QTabWidget *currentTabWidget = nullptr;
131133
ButtonsProvider *buttonsProvider = nullptr;
132-
void updateCurrentTabWidget(QTabWidget *newCurrent);
133134
void equalizeWidths();
134135

135136
private slots:

src/widgets/qmdiSplitTab.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ QWidget *DefaultButtonsProvider::requestButton(bool first, int tabIndex, SplitTa
2525
addNewMdiClient->setAutoRaise(true);
2626
addNewMdiClient->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::DocumentNew));
2727
QObject::connect(addNewMdiClient, &QAbstractButton::clicked, addNewMdiClient,
28-
[manager, addNewMdiClient]() {
28+
[manager, addNewMdiClient, split]() {
29+
auto tab = qobject_cast<QTabWidget *>(addNewMdiClient->parentWidget());
30+
if (tab) {
31+
split->updateCurrentTabWidget(tab);
32+
}
2933
if (manager) {
3034
emit manager->newFileRequested(addNewMdiClient);
3135
}
@@ -142,8 +146,7 @@ void qmdiSplitTab::addClient(qmdiClient *client) {
142146
auto w = dynamic_cast<QWidget *>(client);
143147

144148
if (w == nullptr) {
145-
qDebug("%s %s %d: warning trying to add a qmdiClient which does not derive "
146-
"QWidget",
149+
qDebug("%s %s %d: warning trying to add a qmdiClient which does not derive QWidget",
147150
__FILE__, __FUNCTION__, __LINE__);
148151
return;
149152
}

0 commit comments

Comments
 (0)