| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef GUI_TOOLBARMANAGER_H |
| 25 |
#define GUI_TOOLBARMANAGER_H |
| 26 |
|
| 27 |
#include <string> |
| 28 |
#include <boost/signals2.hpp> |
| 29 |
|
| 30 |
#include <QStringList> |
| 31 |
#include <QPointer> |
| 32 |
#include <QTimer> |
| 33 |
#include <QToolBar> |
| 34 |
#include <QPointer> |
| 35 |
|
| 36 |
#include <FCGlobal.h> |
| 37 |
#include <Base/Parameter.h> |
| 38 |
|
| 39 |
class QAction; |
| 40 |
class QLayout; |
| 41 |
class QMenu; |
| 42 |
class QMouseEvent; |
| 43 |
|
| 44 |
namespace Gui |
| 45 |
{ |
| 46 |
|
| 47 |
class ToolBarAreaWidget; |
| 48 |
enum class ToolBarArea; |
| 49 |
|
| 50 |
class GuiExport ToolBarItem |
| 51 |
{ |
| 52 |
public: |
| 53 |
/** Manages the default visibility status of a toolbar item, as well as the default status |
| 54 |
* of the toggleViewAction usable by the contextual menu to enable and disable its visibility |
| 55 |
*/ |
| 56 |
enum class DefaultVisibility |
| 57 |
{ |
| 58 |
Visible, // toolbar is hidden by default, visibility toggle action is enabled |
| 59 |
Hidden, // toolbar hidden by default, visibility toggle action is enabled |
| 60 |
Unavailable, // toolbar visibility is managed independently by client code and defaults to |
| 61 |
// hidden, visibility toggle action is disabled by default (it is unavailable |
| 62 |
// to the UI). Upon being forced to be available, these toolbars default to |
| 63 |
// visible. |
| 64 |
}; |
| 65 |
|
| 66 |
ToolBarItem(); |
| 67 |
explicit ToolBarItem( |
| 68 |
ToolBarItem* item, |
| 69 |
DefaultVisibility visibilityPolicy = DefaultVisibility::Visible |
| 70 |
); |
| 71 |
~ToolBarItem(); |
| 72 |
|
| 73 |
void setCommand(const std::string&); |
| 74 |
const std::string& command() const; |
| 75 |
|
| 76 |
bool hasItems() const; |
| 77 |
ToolBarItem* findItem(const std::string&); |
| 78 |
ToolBarItem* copy() const; |
| 79 |
uint count() const; |
| 80 |
|
| 81 |
void appendItem(ToolBarItem* item); |
| 82 |
bool insertItem(ToolBarItem*, ToolBarItem* item); |
| 83 |
void removeItem(ToolBarItem* item); |
| 84 |
void clear(); |
| 85 |
|
| 86 |
ToolBarItem& operator<<(ToolBarItem* item); |
| 87 |
ToolBarItem& operator<<(const std::string& command); |
| 88 |
QList<ToolBarItem*> getItems() const; |
| 89 |
|
| 90 |
DefaultVisibility visibilityPolicy; |
| 91 |
|
| 92 |
private: |
| 93 |
std::string _name; |
| 94 |
QList<ToolBarItem*> _items; |
| 95 |
}; |
| 96 |
|
| 97 |
class ToolBarGrip: public QWidget |
| 98 |
{ |
| 99 |
Q_OBJECT |
| 100 |
|
| 101 |
public: |
| 102 |
explicit ToolBarGrip(QToolBar*); |
| 103 |
|
| 104 |
void attach(); |
| 105 |
void detach(); |
| 106 |
|
| 107 |
bool isAttached() const; |
| 108 |
|
| 109 |
protected: |
| 110 |
void paintEvent(QPaintEvent*); |
| 111 |
void mouseMoveEvent(QMouseEvent*); |
| 112 |
void mousePressEvent(QMouseEvent*); |
| 113 |
void mouseReleaseEvent(QMouseEvent*); |
| 114 |
|
| 115 |
void updateSize(); |
| 116 |
|
| 117 |
private: |
| 118 |
QPointer<QAction> _action = nullptr; |
| 119 |
}; |
| 120 |
|
| 121 |
/** |
| 122 |
* QToolBar from Qt lacks few abilities like ability to float toolbar from code. |
| 123 |
* This class allows us to provide custom behaviors for toolbars if needed. |
| 124 |
*/ |
| 125 |
class GuiExport ToolBar: public QToolBar |
| 126 |
{ |
| 127 |
Q_OBJECT |
| 128 |
|
| 129 |
friend class ToolBarGrip; |
| 130 |
|
| 131 |
public: |
| 132 |
ToolBar(); |
| 133 |
explicit ToolBar(QWidget* parent); |
| 134 |
|
| 135 |
virtual ~ToolBar() = default; |
| 136 |
|
| 137 |
void undock(); |
| 138 |
void updateCustomGripVisibility(); |
| 139 |
|
| 140 |
protected: |
| 141 |
void setupConnections(); |
| 142 |
}; |
| 143 |
|
| 144 |
/** |
| 145 |
* The ToolBarManager class is responsible for the creation of toolbars and appending them |
| 146 |
* to the main window. |
| 147 |
* @see ToolBoxManager |
| 148 |
* @see MenuManager |
| 149 |
* @author Werner Mayer |
| 150 |
*/ |
| 151 |
class GuiExport ToolBarManager: public QObject |
| 152 |
{ |
| 153 |
Q_OBJECT |
| 154 |
public: |
| 155 |
enum class State |
| 156 |
{ |
| 157 |
ForceHidden, // Forces a toolbar to hide and hides the toggle action |
| 158 |
ForceAvailable, // Forces a toolbar toggle action to show, visibility depends on user config |
| 159 |
RestoreDefault, // Restores a toolbar toggle action default, visibility as user config |
| 160 |
SaveState, // Saves the state of the toolbars |
| 161 |
}; |
| 162 |
|
| 163 |
/// The one and only instance. |
| 164 |
static ToolBarManager* getInstance(); |
| 165 |
static void destruct(); |
| 166 |
/** Sets up the toolbars of a given workbench. */ |
| 167 |
void setup(ToolBarItem*); |
| 168 |
void saveState() const; |
| 169 |
void restoreState() const; |
| 170 |
void retranslate() const; |
| 171 |
|
| 172 |
bool areToolBarsLocked() const; |
| 173 |
void setToolBarsLocked(bool locked) const; |
| 174 |
|
| 175 |
void setState(const QList<QString>& names, State state); |
| 176 |
void setState(const QString& name, State state); |
| 177 |
|
| 178 |
int toolBarIconSize(QWidget* widget = nullptr) const; |
| 179 |
void setupToolBarIconSize(); |
| 180 |
|
| 181 |
ToolBarArea toolBarArea(QWidget* toolBar) const; |
| 182 |
ToolBarAreaWidget* toolBarAreaWidget(QWidget* toolBar) const; |
| 183 |
|
| 184 |
protected: |
| 185 |
void setup(ToolBarItem*, QToolBar*) const; |
| 186 |
|
| 187 |
void setMovable(bool movable) const; |
| 188 |
|
| 189 |
ToolBarItem::DefaultVisibility getToolbarPolicy(const QToolBar*) const; |
| 190 |
|
| 191 |
bool addToolBarToArea(QObject* source, QMouseEvent* ev); |
| 192 |
bool showContextMenu(QObject* source); |
| 193 |
void onToggleStatusBarWidget(QWidget* widget, bool visible); |
| 194 |
void setToolBarIconSize(QToolBar* toolbar); |
| 195 |
void onTimer(); |
| 196 |
|
| 197 |
bool eventFilter(QObject* source, QEvent* ev) override; |
| 198 |
|
| 199 |
/** Returns a list of all currently existing toolbars. */ |
| 200 |
QList<ToolBar*> toolBars() const; |
| 201 |
ToolBar* findToolBar(const QList<ToolBar*>&, const QString&) const; |
| 202 |
QAction* findAction(const QList<QAction*>&, const QString&) const; |
| 203 |
ToolBarManager(); |
| 204 |
~ToolBarManager() override; |
| 205 |
|
| 206 |
private: |
| 207 |
void setupParameters(); |
| 208 |
void setupStatusBar(); |
| 209 |
void setupMenuBar(); |
| 210 |
void setupConnection(); |
| 211 |
void setupTimer(); |
| 212 |
void setupSizeTimer(); |
| 213 |
void setupResizeTimer(); |
| 214 |
void setupMenuBarTimer(); |
| 215 |
void setupWidgetProducers(); |
| 216 |
|
| 217 |
void addToMenu(QLayout* layout, QWidget* area, QMenu* menu); |
| 218 |
QLayout* findLayoutOfObject(QObject* source, QWidget* area) const; |
| 219 |
ToolBarAreaWidget* findToolBarAreaWidget() const; |
| 220 |
|
| 221 |
private: |
| 222 |
QStringList toolbarNames; |
| 223 |
static ToolBarManager* _instance; |
| 224 |
|
| 225 |
QTimer timer; |
| 226 |
QTimer menuBarTimer; |
| 227 |
QTimer sizeTimer; |
| 228 |
QTimer resizeTimer; |
| 229 |
boost::signals2::scoped_connection connParam; |
| 230 |
ToolBarAreaWidget* statusBarAreaWidget = nullptr; |
| 231 |
ToolBarAreaWidget* menuBarLeftAreaWidget = nullptr; |
| 232 |
ToolBarAreaWidget* menuBarRightAreaWidget = nullptr; |
| 233 |
ParameterGrp::handle hGeneral; |
| 234 |
ParameterGrp::handle hPref; |
| 235 |
ParameterGrp::handle hStatusBar; |
| 236 |
ParameterGrp::handle hMenuBarLeft; |
| 237 |
ParameterGrp::handle hMenuBarRight; |
| 238 |
std::map<QToolBar*, QPointer<QToolBar>> resizingToolbars; |
| 239 |
int _toolBarIconSize = 0; |
| 240 |
int _statusBarIconSize = 0; |
| 241 |
int _menuBarIconSize = 0; |
| 242 |
bool blockRestore = false; |
| 243 |
}; |
| 244 |
|
| 245 |
} // namespace Gui |
| 246 |
|
| 247 |
|
| 248 |
#endif // GUI_TOOLBARMANAGER_H |