diff --git a/src/gui/widgets/hidingtabwidget.cpp b/src/gui/widgets/hidingtabwidget.cpp index 9ed91ee4..1a5a21c7 100644 --- a/src/gui/widgets/hidingtabwidget.cpp +++ b/src/gui/widgets/hidingtabwidget.cpp @@ -1,157 +1,195 @@ /*************************************************************************** - * Copyright (C) 2004-2017 by Thomas Fischer * + * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #include "hidingtabwidget.h" +#include + +typedef struct { + /// the hidden widget + QWidget *widget; + /// the hidden widget's neighboring widgets, + /// used to find a place where to insert the tab when it will be shown again. + QWidget *leftNeighborWidget, *rightNeighborWidget; + /// tab properties + QIcon icon; + QString label; + bool enabled; + QString toolTip; + QString whatsThis; +} HiddenTabInfo; + +class HidingTabWidget::Private { +private: + HidingTabWidget *parent; + QTabWidget *parentAsQTabWidget; + +public: + QSet hiddenTabInfo; + + Private(HidingTabWidget *_parent) + : parent(_parent), parentAsQTabWidget(qobject_cast(_parent)) + { + /// nothing + } + + int showTab(const HiddenTabInfo &hti, int index = InvalidTabPosition) + { + if (index <0) { + /// No position to insert tab given, so make an educated guess + index = parent->count(); ///< Append at end of tab row by default + int i = InvalidTabPosition; + if ((i = parent->indexOf(hti.leftNeighborWidget)) >= 0) + index = i + 1; ///< Right of left neighbor + else if ((i = parent->indexOf(hti.rightNeighborWidget)) >= 0) + index = i; ///< Left of right neighbor + } + + /// Insert tab using QTabWidget's original function + index = parentAsQTabWidget->insertTab(index, hti.widget, hti.icon, hti.label); + /// Restore tab's properties + parent->setTabToolTip(index, hti.toolTip); + parent->setTabWhatsThis(index, hti.whatsThis); + parent->setTabEnabled(index, hti.enabled); + + return index; + } +}; + + /// required to for QSet -uint qHash(const HidingTabWidget::HiddenTabInfo &hti) +uint qHash(const HiddenTabInfo &hti) { return qHash(hti.widget); } /// required to for QSet -bool operator==(const HidingTabWidget::HiddenTabInfo &a, const HidingTabWidget::HiddenTabInfo &b) +bool operator==(const HiddenTabInfo &a, const HiddenTabInfo &b) { return a.widget == b.widget; } const int HidingTabWidget::InvalidTabPosition = -1; HidingTabWidget::HidingTabWidget(QWidget *parent) - : QTabWidget(parent) + : QTabWidget(parent), d(new Private(this)) { /// nothing to see here } +HidingTabWidget::~HidingTabWidget() +{ + delete d; +} + QWidget *HidingTabWidget::hideTab(int index) { if (index < 0 || index >= count()) return nullptr; HiddenTabInfo hti; hti.widget = widget(index); hti.leftNeighborWidget = index > 0 ? widget(index - 1) : nullptr; hti.rightNeighborWidget = index < count() - 1 ? widget(index + 1) : nullptr; hti.label = tabText(index); hti.icon = tabIcon(index); hti.enabled = isTabEnabled(index); hti.toolTip = tabToolTip(index); hti.whatsThis = tabWhatsThis(index); - m_hiddenTabInfo.insert(hti); + d->hiddenTabInfo.insert(hti); QTabWidget::removeTab(index); return hti.widget; } int HidingTabWidget::showTab(QWidget *page) { - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) - return showTab(hti); + return d->showTab(hti); } return InvalidTabPosition; } -int HidingTabWidget::showTab(const HiddenTabInfo &hti, int index) -{ - if (index == InvalidTabPosition) { - index = count(); ///< append at end of tab row - int i = InvalidTabPosition; - if ((i = indexOf(hti.leftNeighborWidget)) >= 0) - index = i + 1; ///< right of left neighbor - else if ((i = indexOf(hti.rightNeighborWidget)) >= 0) - index = i; ///< left of right neighbor - } - - /// insert tab using KTabWidget's original function - index = QTabWidget::insertTab(index, hti.widget, hti.icon, hti.label); - /// restore tab's properties - setTabToolTip(index, hti.toolTip); - setTabWhatsThis(index, hti.whatsThis); - setTabEnabled(index, hti.enabled); - - return index; -} - void HidingTabWidget::removeTab(int index) { if (index >= 0 && index < count()) { QWidget *page = widget(index); - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) { - m_hiddenTabInfo.remove(hti); + d->hiddenTabInfo.remove(hti); break; } } QTabWidget::removeTab(index); } } int HidingTabWidget::addTab(QWidget *page, const QString &label) { - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) { - int pos = showTab(hti); + int pos = d->showTab(hti); setTabText(pos, label); return pos; } } return QTabWidget::addTab(page, label); } int HidingTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label) { - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) { - int pos = showTab(hti); + int pos = d->showTab(hti); setTabIcon(pos, icon); setTabText(pos, label); return pos; } } return QTabWidget::addTab(page, icon, label); } int HidingTabWidget::insertTab(int index, QWidget *page, const QString &label) { - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) { - int pos = showTab(hti, index); + int pos = d->showTab(hti, index); setTabText(pos, label); return pos; } } return QTabWidget::insertTab(index, page, label); } int HidingTabWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label) { - for (const HiddenTabInfo &hti : const_cast &>(m_hiddenTabInfo)) { + for (const HiddenTabInfo &hti : const_cast &>(d->hiddenTabInfo)) { if (hti.widget == page) { - index = showTab(hti, index); + index = d->showTab(hti, index); setTabIcon(index, icon); setTabText(index, label); return index; } } return QTabWidget::insertTab(index, page, icon, label); } diff --git a/src/gui/widgets/hidingtabwidget.h b/src/gui/widgets/hidingtabwidget.h index bd7ef4f4..51239325 100644 --- a/src/gui/widgets/hidingtabwidget.h +++ b/src/gui/widgets/hidingtabwidget.h @@ -1,104 +1,89 @@ /*************************************************************************** - * Copyright (C) 2004-2017 by Thomas Fischer * + * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #ifndef KBIBTEX_GUI_HIDINGTABWIDGET_H #define KBIBTEX_GUI_HIDINGTABWIDGET_H -#include #include /** * @brief The HidingTabWidget class to hide and show tabs in a QTabWidget. * * This class extends the original QTabWidget by the feature of hiding and showing * tabs previously added or inserted. * * @author Thomas Fischer */ class HidingTabWidget : public QTabWidget { Q_OBJECT public: /// Negative value to describe an invalid tab position static const int InvalidTabPosition; explicit HidingTabWidget(QWidget *parent = nullptr); + ~HidingTabWidget(); /** * Hides the tab at position @param index from this stack of widgets. * The page widget itself is not deleted. * For future reference, the page widget is returned. - * If @param index does not refer to a valid widget, NULL will be returned. + * If @param index does not refer to a valid widget, nullptr will be returned. */ QWidget *hideTab(int index); /** * Shows a previously hidden tab reusing its properties (label, icon, tooltip, what's this, enbled). * If possible, the hidden tab will be shown between its previous neighbor tabs. * If the provided @param page was not added by @see addTab or @see insertTab, * the function will return InvalidTabPosition. If the tab could be shown again, its new position will be returned. * The behavior is undefined if @param page is not a value returned by a previous @see hideTab call. */ int showTab(QWidget *page); /** * Reimplemented from QTabWidget, same semantics. */ void removeTab(int index); /** * Reimplemented from QTabWidget, same semantics. */ int addTab(QWidget *page, const QString &label); /** * Reimplemented from QTabWidget, same semantics. */ int addTab(QWidget *page, const QIcon &icon, const QString &label); /** * Reimplemented from QTabWidget, same semantics. */ int insertTab(int index, QWidget *page, const QString &label); /** * Reimplemented from QTabWidget, same semantics. */ int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label); - typedef struct { - /// the hidden widget - QWidget *widget; - /// the hidden widget's neighboring widgets, - /// used to find a place where to insert the tab when it will be shown again. - QWidget *leftNeighborWidget, *rightNeighborWidget; - /// tab properties - QIcon icon; - QString label; - bool enabled; - QString toolTip; - QString whatsThis; - } HiddenTabInfo; - private: - QSet m_hiddenTabInfo; - - int showTab(const HiddenTabInfo &hti, int index = InvalidTabPosition); + class Private; + Private *const d; }; #endif // KBIBTEX_GUI_HIDINGTABWIDGET_H