File indexing completed on 2024-05-12 16:35:47

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Ariya Hidayat <ariya@kde.org>
0003    Copyright (C) 2003 Norbert Andres <nandres@web.de>
0004    Copyright (C) 2002 Laurent Montel <montel@kde.org>
0005    Copyright (C) 1999 David Faure <faure@kde.org>
0006    Copyright (C) 1999 Boris Wedl <boris.wedl@kfunigraz.ac.at>
0007    Copyright (C) 1998-2000 Torben Weis <weis@kde.org>
0008 
0009    This library is free software; you can redistribute it and/or
0010    modify it under the terms of the GNU Library General Public
0011    License as published by the Free Software Foundation; either
0012    version 2 of the License, or (at your option) any later version.
0013 
0014    This library is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017    Library General Public License for more details.
0018 
0019    You should have received a copy of the GNU Library General Public License
0020    along with this library; see the file COPYING.LIB.  If not, write to
0021    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0022  * Boston, MA 02110-1301, USA.
0023 */
0024 
0025 #ifndef TABBAR_H
0026 #define TABBAR_H
0027 
0028 #include <QWidget>
0029 #include <QStringList>
0030 #include <QMouseEvent>
0031 #include <QWheelEvent>
0032 #include <QResizeEvent>
0033 #include <QPaintEvent>
0034 
0035 namespace Calligra
0036 {
0037 namespace Sheets
0038 {
0039 class TabBarPrivate;
0040 
0041 /**
0042  * The TabBar class provides a tab bar, for use to switch active
0043  * page/sheet in a document.
0044  *
0045  * The tab bar is typically used in the main view.
0046  * It is the small widget on the bottom left corner.
0047  * Pages/sheets are displayed as tabs, clicking on
0048  * one of the tab will activate the corresponding sheet (this is actually
0049  * done in main view). Current active page/sheet is marked by bold text.
0050  *
0051  * The tab bar supports page/sheet reorder by dragging a certain tab
0052  * and move it to another location. The main view should handle the necessary
0053  * action to perform the actual reorder.
0054  *
0055  * There are four scroll buttons available in the tab bar. They are used
0056  * to shift the tabs in left and right direction, for example when there
0057  * are more tabs than the space available to display all tabs.
0058  *
0059  * Since a page/sheet can be hidden, the tab bar only shows the visible page/sheet.
0060  * When a hidden page or sheet is shown again, it will be on the same position as
0061  * before it was hidden.
0062  *
0063  * When the document is protected, it is necessary to set the tab bar as
0064  * read-only using setReadOnly (see also readOnly). If it is set to true,
0065  * tabs can not be moved by dragging and context menu will not be displayed.
0066  *
0067  * @short A bar with tabs and scroll buttons.
0068  */
0069 class TabBar : public QWidget
0070 {
0071     Q_OBJECT
0072 
0073     Q_PROPERTY(QString activeTab READ activeTab WRITE setActiveTab)
0074     Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly)
0075     Q_PROPERTY(QStringList tabs READ tabs WRITE setTabs)
0076     Q_PROPERTY(unsigned count READ count)
0077 
0078 public:
0079 
0080     /**
0081      * Creates a new tabbar.
0082      */
0083     explicit TabBar(QWidget* parent = 0, const char *name = 0);
0084 
0085     /**
0086      * Destroy the tabbar.
0087      */
0088     ~TabBar() override;
0089 
0090     /**
0091      * Returns true if the tab bar is read only.
0092      */
0093     bool readOnly() const;
0094 
0095     /**
0096      * Returns all the tab as list of strings.
0097      */
0098     QStringList tabs() const;
0099 
0100     /**
0101      * Returns number of tabs.
0102      * This is the same as TabBar::tabs().count()
0103      */
0104     unsigned count() const;
0105 
0106     /**
0107      * Returns the active tab.
0108      */
0109     QString activeTab() const;
0110 
0111     /**
0112      * Returns true if it is possible to scroll one tab back.
0113      *
0114      * \sa scrollBack
0115      */
0116     bool canScrollBack() const;
0117 
0118     /**
0119      * Returns true if it is possible to scroll one tab forward.
0120      *
0121      * \sa scrollForward
0122      */
0123     bool canScrollForward() const;
0124 
0125     /**
0126      * Ensures that specified tab is visible.
0127      */
0128     void ensureVisible(const QString& tab);
0129 
0130 public Q_SLOTS:
0131 
0132     /**
0133      * Replaces all tabs with the list of strings.
0134      */
0135     void setTabs(const QStringList& list);
0136 
0137     /**
0138      * Sets the tab bar to be read only.
0139      *
0140      * If the tab bar is read only, tab reordering is not allowed.
0141      * This means that signal tabMoved, contextMenu and doubleClicked
0142      * would not be emitted.
0143      */
0144     void setReadOnly(bool ro);
0145 
0146     /**
0147      * Adds a tab to the tab bar.
0148      */
0149     void addTab(const QString& text);
0150 
0151     /**
0152      * Removes a tab from the bar. If the tab was the active one then
0153      * no tab will be active.
0154      * It is recommended to call setActiveTab after a call to this function.
0155      */
0156     void removeTab(const QString& text);
0157 
0158     /**
0159      * Renames a tab.
0160      */
0161     void renameTab(const QString& old_name, const QString& new_name);
0162 
0163     /**
0164      * Moves a tab to another position and reorder other tabs.
0165      *
0166      * Example 1: if there are four tabs A - B - C - D, then
0167      * moveTab(2,1) will yield A - C - B - D. This is because
0168      * 2nd tab (i.e C) is moved to a position before 1th tab (i.e B).
0169      *
0170      * Example 2: for these tabs: X - Y - Z, moveTab(0,3) will
0171      * move tab X after tab Z so that the result is Y - Z - X.
0172      */
0173     void moveTab(int tab, int target);
0174 
0175     /**
0176      * Scrolls one tab back. Does nothing if the leftmost tab (rightmost tab
0177      * for right-to-left layouts) is already the first tab.
0178      *
0179      * \sa canScrollBack
0180      */
0181     void scrollBack();
0182 
0183     /**
0184      * Scrolls one tab forward. Does nothing if the rightmost tab (leftmost tab
0185      * for right-to-left layouts) is already the last tab.
0186      *
0187      * \sa canScrollForward
0188      */
0189     void scrollForward();
0190 
0191     /**
0192      * Scrolls to the first tab. Does nothing if the leftmost tab (rightmost tab
0193      * for right-to-left layouts) is already the first tab.
0194      *
0195      * \sa canScrollBack
0196      */
0197     void scrollFirst();
0198 
0199     /**
0200      * Scrolls to the last tab. Does nothing if the rightmost tab (leftmost tab
0201      * for right-to-left layouts) is already the last tab.
0202      *
0203      * \sa canScrollForward
0204      */
0205     void scrollLast();
0206 
0207     /**
0208      * Sets active tab.
0209      */
0210     void setActiveTab(const QString& text);
0211 
0212     /**
0213      * Removes all tabs.
0214      */
0215     void clear();
0216 
0217     QSize sizeHint() const override;
0218 
0219 Q_SIGNALS:
0220 
0221     /**
0222      * Emitted if the active tab changed.
0223      */
0224     void tabChanged(const QString& _text);
0225 
0226     /**
0227      * This signal is emitted whenever a tab is dragged, moved and
0228      * released. This means that the user wants to move a tab into
0229      * another position (right before target).
0230      *
0231      * When the signal is emitted, the tabs are not reordered.
0232      * Therefore if you just ignore this signal, than no tab reorder
0233      * is possible. Call moveTab (from the slot connected to this signal)
0234      * to perform the actual tab reorder.
0235      */
0236     void tabMoved(unsigned tab, unsigned target);
0237 
0238     /**
0239      * This signal is emitted whenever the tab bar is right-clicked.
0240      * Typically it is used to popup a context menu.
0241      */
0242     void contextMenu(const QPoint& pos);
0243 
0244     /**
0245      * This signal is emitted whenever the tab bar is double-clicked.
0246      */
0247     void doubleClicked();
0248 
0249 protected Q_SLOTS:
0250     void autoScrollBack();
0251     void autoScrollForward();
0252 
0253 protected:
0254     void paintEvent(QPaintEvent* ev) override;
0255     void resizeEvent(QResizeEvent* ev) override;
0256     void mousePressEvent(QMouseEvent* ev) override;
0257     void mouseReleaseEvent(QMouseEvent* ev) override;
0258     void mouseDoubleClickEvent(QMouseEvent* ev) override;
0259     void mouseMoveEvent(QMouseEvent* ev) override;
0260     void wheelEvent(QWheelEvent * e) override;
0261 
0262 private:
0263     TabBarPrivate * const d;
0264 
0265     // don't allow copy or assignment
0266     TabBar(const TabBar&);
0267     TabBar& operator=(const TabBar&);
0268 };
0269 
0270 } // namespace Sheets
0271 } // namespace Calligra
0272 
0273 #endif // TABBAR_H