File indexing completed on 2024-05-12 04:58:28

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef TOOLBUTTON_H
0019 #define TOOLBUTTON_H
0020 
0021 #include <QToolButton>
0022 #include <QTimer>
0023 
0024 #include "qzcommon.h"
0025 
0026 class FALKON_EXPORT ToolButton : public QToolButton
0027 {
0028     Q_OBJECT
0029 
0030     Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize)
0031     Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth)
0032     Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight)
0033     Q_PROPERTY(QImage multiIcon READ multiIcon WRITE setMultiIcon)
0034     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
0035     Q_PROPERTY(QString themeIcon READ themeIcon WRITE setThemeIcon)
0036     Q_PROPERTY(QIcon fallbackIcon READ fallbackIcon WRITE setFallbackIcon)
0037 
0038 public:
0039     explicit ToolButton(QWidget* parent = nullptr);
0040 
0041     // MultiIcon - Image containing pixmaps for all button states
0042     QImage multiIcon() const;
0043     void setMultiIcon(const QImage &image);
0044 
0045     // ThemeIcon - Standard QToolButton with theme icon
0046     QString themeIcon() const;
0047     void setThemeIcon(const QString &icon);
0048 
0049     // FallbackIcon - In case theme doesn't contain ThemeIcon
0050     QIcon fallbackIcon() const;
0051     void setFallbackIcon(const QIcon &fallbackIcon);
0052 
0053     // Icon - Standard QToolButton with icon
0054     QIcon icon() const;
0055     void setIcon(const QIcon &icon);
0056 
0057     // Menu - Menu is handled in ToolButton and is not passed to QToolButton
0058     // There won't be menu indicator shown in the button
0059     // QToolButton::MenuButtonPopup is not supported
0060     QMenu* menu() const;
0061     void setMenu(QMenu* menu);
0062 
0063     // Align the right corner of menu to the right corner of button
0064     bool showMenuInside() const;
0065     void setShowMenuInside(bool enable);
0066 
0067     // Show button menu on right click
0068     bool showMenuOnRightClick() const;
0069     void setShowMenuOnRightClick(bool enable);
0070 
0071     // Set the button to look as it was in toolbar
0072     // (it now only sets the correct icon size)
0073     bool toolbarButtonLook() const;
0074     void setToolbarButtonLook(bool enable);
0075 
0076 Q_SIGNALS:
0077     void middleMouseClicked();
0078     void controlClicked();
0079     void doubleClicked();
0080 
0081     // It is needed to use these signals with ShowMenuInside
0082     void aboutToShowMenu();
0083     void aboutToHideMenu();
0084 
0085 private Q_SLOTS:
0086     void menuAboutToHide();
0087     void showMenu();
0088 
0089 protected:
0090     void mousePressEvent(QMouseEvent* e) override;
0091     void mouseReleaseEvent(QMouseEvent* e) override;
0092     void mouseDoubleClickEvent(QMouseEvent* e) override;
0093     void contextMenuEvent(QContextMenuEvent *e) override;
0094     void paintEvent(QPaintEvent* e) override;
0095 
0096 private:
0097     QImage m_multiIcon;
0098     QString m_themeIcon;
0099     QTimer m_pressTimer;
0100     QMenu* m_menu;
0101 
0102     enum Options {
0103         MultiIconOption = 1,
0104         ShowMenuInsideOption = 2,
0105         ToolBarLookOption = 4,
0106         ShowMenuOnRightClick = 8
0107     };
0108     Q_DECLARE_FLAGS(OptionsFlags, Options)
0109     OptionsFlags m_options;
0110 };
0111 
0112 
0113 #endif // TOOLBUTTON_H