Skip to content

Commit e854ee3

Browse files
committed
Fix moving tab group with items in tree
This fixes moving "c" (tab with item) to "b" (group) in the following tab tree: a └──b └──c └──d The result should be: a └──c └──d The problem was that "b" group was empty while iterating the tree, which broke the iteration process. The fix is to delete all empty groups after the iteration.
1 parent d81740c commit e854ee3

File tree

3 files changed

+71
-44
lines changed

3 files changed

+71
-44
lines changed

src/gui/mainwindow.cpp

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,13 +1530,6 @@ QAction *MainWindow::addTrayAction(Actions::Id id)
15301530
return act;
15311531
}
15321532

1533-
void MainWindow::updateTabIcon(const QString &newName, const QString &oldName)
1534-
{
1535-
const QString icon = getIconNameForTabName(oldName);
1536-
if ( !icon.isEmpty() )
1537-
setIconNameForTabName(newName, icon);
1538-
}
1539-
15401533
template <typename Receiver, typename ReturnType>
15411534
QAction *MainWindow::addItemAction(Actions::Id id, Receiver *receiver, ReturnType (Receiver::* slot)())
15421535
{
@@ -3134,24 +3127,25 @@ void MainWindow::doSaveTabPositions(AppConfig *appConfig)
31343127

31353128
void MainWindow::tabsMoved(const QString &oldPrefix, const QString &newPrefix)
31363129
{
3137-
const QStringList tabs = ui->tabWidget->tabs();
3138-
Q_ASSERT( oldPrefix == newPrefix || !tabs.contains(oldPrefix) );
3139-
Q_ASSERT( !tabs.contains(QString()) );
3130+
const QStringList newTabNames = ui->tabWidget->tabs();
3131+
Q_ASSERT( oldPrefix == newPrefix || !newTabNames.contains(oldPrefix) );
3132+
Q_ASSERT( !newTabNames.contains(QString()) );
31403133

3141-
const QString prefix = oldPrefix + '/';
3134+
AppConfig appConfig;
3135+
Tabs tabs;
31423136

31433137
// Rename tabs if needed.
3144-
for (int i = 0 ; i < tabs.size(); ++i) {
3138+
for (int i = 0 ; i < newTabNames.size(); ++i) {
3139+
const QString &newTabName = newTabNames[i];
31453140
auto placeholder = getPlaceholder(i);
31463141
const QString oldTabName = placeholder->tabName();
3147-
3148-
if ( (oldTabName == oldPrefix || oldTabName.startsWith(prefix)) && newPrefix != oldPrefix) {
3149-
const QString newName = newPrefix + oldTabName.mid(oldPrefix.size());
3150-
setTabName(placeholder, newName);
3151-
}
3142+
if (newTabName != oldTabName)
3143+
updateTabName(placeholder, newTabName, &appConfig, &tabs);
31523144
}
31533145

3154-
saveTabPositions();
3146+
const QStringList tabNames = ui->tabWidget->tabs();
3147+
tabs.save(&appConfig.settings(), tabNames);
3148+
appConfig.setOption(Config::tabs::name(), tabNames);
31553149
}
31563150

31573151
void MainWindow::tabBarMenuRequested(QPoint pos, int tab)
@@ -3480,31 +3474,34 @@ void MainWindow::onItemDoubleClicked()
34803474
activateCurrentItem();
34813475
}
34823476

3483-
bool MainWindow::setTabName(ClipboardBrowserPlaceholder *placeholder, const QString &newName)
3477+
bool MainWindow::updateTabName(
3478+
ClipboardBrowserPlaceholder *placeholder,
3479+
const QString &newName,
3480+
AppConfig *appConfig,
3481+
Tabs *tabs)
34843482
{
34853483
const QString oldName = placeholder->tabName();
34863484
if ( !placeholder->setTabName(newName) )
34873485
return false;
34883486

3489-
updateTabIcon(newName, oldName);
3490-
const int tabIndex = findTabIndex(oldName);
3491-
Q_ASSERT(tabIndex != -1);
3492-
ui->tabWidget->setTabName(tabIndex, newName);
3493-
3494-
Tabs tabs;
3495-
TabProperties tabProperties = tabs.tabProperties(oldName);
3487+
TabProperties tabProperties = tabs->tabProperties(oldName);
34963488
tabProperties.name = newName;
3497-
tabs.setTabProperties(tabProperties);
3498-
Settings settings;
3499-
const QStringList tabNames = ui->tabWidget->tabs();
3500-
tabs.save(&settings, tabNames);
3501-
settings.setValue("Options/tabs", tabNames);
3502-
3503-
if (oldName == m_options.clipboardTab)
3504-
settings.setValue("Options/clipboard_tab", newName);
3489+
tabs->setTabProperties(tabProperties);
3490+
3491+
if (oldName == m_options.clipboardTab) {
3492+
m_options.clipboardTab = newName;
3493+
appConfig->setOption(Config::clipboard_tab::name(), newName);
3494+
// Restart clipboard monitoring to apply new clipboard tab configuration.
3495+
if (!m_clipboardStoringDisabled) {
3496+
emit disableClipboardStoring(true);
3497+
emit disableClipboardStoring(false);
3498+
}
3499+
}
35053500

3506-
if (oldName == m_options.trayTabName)
3507-
settings.setValue("Options/tray_tab", newName);
3501+
if (oldName == m_options.trayTabName) {
3502+
m_options.trayTabName = newName;
3503+
appConfig->setOption(Config::tray_tab::name(), newName);
3504+
}
35083505

35093506
return true;
35103507
}
@@ -4211,8 +4208,19 @@ void MainWindow::renameTab(const QString &name, int tabIndex)
42114208
return;
42124209

42134210
auto placeholder = getPlaceholder(tabIndex);
4214-
if (placeholder)
4215-
setTabName(placeholder, name);
4211+
if (!placeholder)
4212+
return;
4213+
4214+
AppConfig appConfig;
4215+
Tabs tabs;
4216+
if ( !updateTabName(placeholder, name, &appConfig, &tabs) )
4217+
return;
4218+
4219+
ui->tabWidget->setTabName(tabIndex, name);
4220+
4221+
const QStringList tabNames = ui->tabWidget->tabs();
4222+
tabs.save(&appConfig.settings(), tabNames);
4223+
appConfig.setOption(Config::tabs::name(), tabNames);
42164224
}
42174225

42184226
void MainWindow::removeTabGroup(const QString &name)

src/gui/mainwindow.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ class MainWindow final : public QMainWindow
430430

431431
void sendActionData(int actionId, const QByteArray &bytes);
432432

433+
void clipboardTabChanged();
434+
433435
protected:
434436
bool eventFilter(QObject *object, QEvent *ev) override;
435437
void keyPressEvent(QKeyEvent *event) override;
@@ -588,8 +590,6 @@ class MainWindow final : public QMainWindow
588590

589591
QAction *addTrayAction(Actions::Id id);
590592

591-
void updateTabIcon(const QString &newName, const QString &oldName);
592-
593593
template <typename Receiver, typename ReturnType>
594594
QAction *addItemAction(Actions::Id id, Receiver *receiver, ReturnType (Receiver::* slot)());
595595

@@ -640,7 +640,16 @@ class MainWindow final : public QMainWindow
640640
void onItemClicked();
641641
void onItemDoubleClicked();
642642

643-
bool setTabName(ClipboardBrowserPlaceholder *placeholder, const QString &newName);
643+
/**
644+
* Update tab name in placeholder and configuration.
645+
* Return true on success, false if setting tab name in placeholder failed
646+
* (most likely failure to move the tab data).
647+
*/
648+
bool updateTabName(
649+
ClipboardBrowserPlaceholder *placeholder,
650+
const QString &newName,
651+
AppConfig *appConfig,
652+
Tabs *tabs);
644653

645654
ConfigurationManager *cm;
646655
Ui::MainWindow *ui;

src/gui/tabtree.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,12 @@ void TabTree::dropEvent(QDropEvent *event)
591591

592592
QList<QTreeWidgetItem*> newTabs;
593593
QList<int> indexes;
594+
QList<QPersistentModelIndex> toDelete;
594595
for ( QTreeWidgetItemIterator it(topLevelItem(0)); *it; ++it ) {
595596
auto item = *it;
596597
// Remove empty groups.
597598
if ( isEmptyTabGroup(item) ) {
598-
deleteItem(item);
599+
toDelete.append(indexFromItem(item));
599600
} else {
600601
const int oldIndex = getTabIndex(item);
601602
if (oldIndex != -1) {
@@ -605,10 +606,19 @@ void TabTree::dropEvent(QDropEvent *event)
605606
}
606607
}
607608

608-
m_tabs = std::move(newTabs);
609-
emit tabsMoved(oldPrefix, newPrefix, indexes);
609+
for (const auto &index : toDelete) {
610+
if (!index.isValid())
611+
continue;
612+
613+
QTreeWidgetItem *item = itemFromIndex(index);
614+
if (item)
615+
deleteItem(item);
616+
}
610617

611618
updateSize();
619+
620+
m_tabs = std::move(newTabs);
621+
emit tabsMoved(oldPrefix, newPrefix, indexes);
612622
} else {
613623
event->ignore();
614624
}

0 commit comments

Comments
 (0)