Mudlet  0
Mudclient
TToolBar.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
3  * Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
4  * Copyright (C) 2017 by Stephen Lyons - slysven@virginmedia.com *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program 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 General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20  ***************************************************************************/
21 
22 
23 #include "TToolBar.h"
24 
25 
26 #include "Host.h"
27 #include "TAction.h"
28 #include "TConsole.h"
29 #include "TFlipButton.h"
30 #include "mudlet.h"
31 
32 
33 TToolBar::TToolBar(TAction* pA, const QString& name, QWidget* pW)
34 : QDockWidget( pW )
35 , mpTAction( pA )
36 , mVerticalOrientation( false )
37 , mpWidget( new QWidget( this ) )
38 , mName( name )
39 , mRecordMove( false )
40 , mpLayout( nullptr )
41 , mItemCount( 0 )
42 {
43  setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
44  setWidget(mpWidget);
45  setObjectName(QString("dockToolBar_%1").arg(name));
46 
47  connect(this, &QDockWidget::dockLocationChanged, this, &TToolBar::slot_dockLocationChanged);
48  connect(this, &QDockWidget::topLevelChanged, this, &TToolBar::slot_topLevelChanged);
49 
51  mpLayout = new QGridLayout(mpWidget);
52  setContentsMargins(0, 0, 0, 0);
53  mpLayout->setContentsMargins(0, 0, 0, 0);
54  mpLayout->setSpacing(0);
55  QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
56  mpWidget->setSizePolicy(sizePolicy);
57  }
58  auto test = new QWidget;
59  setTitleBarWidget(test);
60  setStyleSheet(mpTAction->css);
61  mpWidget->setStyleSheet(mpTAction->css);
62 }
63 
64 void TToolBar::resizeEvent(QResizeEvent* e)
65 {
66  if (!mudlet::self()->mIsLoadingLayout) {
68  }
69 }
70 
71 void TToolBar::moveEvent(QMoveEvent* e)
72 {
73  if (!mudlet::self()->mIsLoadingLayout) {
75  }
76 
77  if (mRecordMove) {
78  if (!mpTAction) {
79  return;
80  }
81  mpTAction->mPosX = e->pos().x();
82  mpTAction->mPosY = e->pos().y();
83  }
84  e->ignore();
85 }
86 
87 void TToolBar::slot_dockLocationChanged(Qt::DockWidgetArea dwArea)
88 {
89  if (mpTAction) {
91  }
92 }
93 
94 void TToolBar::slot_topLevelChanged(bool topLevel)
95 {
96  if (mpTAction) {
98  }
99 }
100 
102 {
103  if (!mpTAction->mUseCustomLayout) {
104  QSize size = pB->minimumSizeHint();
105  if (pB->mpTAction->getButtonRotation() > 0) {
106  size.transpose();
107  }
108  pB->setMaximumSize(size);
109  pB->setMinimumSize(size);
110  } else {
111  QSize size = QSize(pB->mpTAction->mSizeX, pB->mpTAction->mSizeY);
112  pB->setMaximumSize(size);
113  pB->setMinimumSize(size);
114  pB->setParent(mpWidget);
115  pB->setGeometry(pB->mpTAction->mPosX, pB->mpTAction->mPosY, pB->mpTAction->mSizeX, pB->mpTAction->mSizeY);
116  }
117 
118  pB->setStyleSheet(pB->mpTAction->css);
119  pB->setFlat(pB->mpTAction->getButtonFlat());
120  int rotation = pB->mpTAction->getButtonRotation();
121  switch (rotation) {
122  case 0:
123  pB->setOrientation(Qt::Horizontal);
124  break;
125  case 1:
126  pB->setOrientation(Qt::Vertical);
127  break;
128  case 2:
129  pB->setOrientation(Qt::Vertical);
130  pB->setMirrored(true);
131  break;
132  }
133 
134  if (!mpTAction->mUseCustomLayout) {
135  // tool bar mButtonColumns > 0 -> autolayout
136  // case == 0: use individual button placement for user defined layouts
137  int columns = mpTAction->getButtonColumns();
138  if (columns <= 0) {
139  columns = 1;
140  }
141  if (columns > 0) {
142  mItemCount++;
143  int row = mItemCount / columns;
144  int col = mItemCount % columns;
145  if (mVerticalOrientation) {
146  mpLayout->addWidget(pB, row, col);
147  } else {
148  mpLayout->addWidget(pB, col, row);
149  }
150  }
151  } else {
152  pB->move(pB->mpTAction->mPosX, pB->mpTAction->mPosY);
153  }
154 
155  // Was using pressed() signal but now we want to track the ACTUAL state of
156  // the underlying QAbstractButton
157  connect(pB, &QAbstractButton::clicked, this, &TToolBar::slot_pressed);
158 }
159 
161 {
163  return;
164  }
165  auto fillerWidget = new QWidget;
166  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
167  fillerWidget->setSizePolicy(sizePolicy);
168  int columns = mpTAction->getButtonColumns();
169  if (columns <= 0) {
170  columns = 1;
171  }
172  int row = (++mItemCount) / columns;
173  int column = (mItemCount - 1) % columns;
174  mpLayout->addWidget(fillerWidget, row, column);
175  // 3 lines above are to avoid order of operations problem of original line
176  // (-Wsequence-point warning on mItemCount) NEEDS TO BE CHECKED:
177  // mpLayout->addWidget( fillerWidget, ++mItemCount/columns, mItemCount%columns );
178 }
179 
180 // Used by buttons directly on a TToolBar instance but NOT on sub-menu item - we
181 // now retrieve the button state to ensure the visible representation is used.
182 void TToolBar::slot_pressed(const bool isChecked)
183 {
184  auto * pB = dynamic_cast<TFlipButton*>(sender());
185  if (!pB) {
186  return;
187  }
188 
189  TAction* pA = pB->mpTAction;
190  // NOTE: This function blocks until an item is selected from the menu, and,
191  // as the action to "pop-up" the menu is the same as "buttons" use to
192  // perform their command/scripts is why "commands" are (no longer) permitted
193  // on a "menu". It also means that the script for a "menu" is run every
194  // time it is "clicked" upon to display the pop-up containing the menu
195  // entries...
196  pB->menu();
197 
198  if (pA->mIsPushDownButton) {
199  pA->mButtonState = isChecked;
200  pA->mpHost->mpConsole->mButtonState = (pA->mButtonState ? 2 : 1); // Was using 1 and 0 but that was wrong
201  } else {
202  pA->mButtonState = false;
203  pB->setChecked(false); // This does NOT invoke the clicked()!
204  pA->mpHost->mpConsole->mButtonState = 1; // Was effectively 0 but that is wrong
205  }
206 
207  pA->execute();
208 }
209 
211 {
212  auto pW = new QWidget(this);
213  setWidget(pW);
214  mpWidget->deleteLater();
215  mpWidget = pW;
216 
217  if (!mpTAction->mUseCustomLayout) {
218  mpLayout = new QGridLayout(mpWidget);
219  mpLayout->setContentsMargins(0, 0, 0, 0);
220  mpLayout->setSpacing(0);
221  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
222  mpWidget->setSizePolicy(sizePolicy);
223  } else {
224  mpLayout = nullptr;
225  }
226  auto test = new QWidget;
227  setStyleSheet(mpTAction->css);
228  mpWidget->setStyleSheet(mpTAction->css);
229  setTitleBarWidget(test);
230 
231  mudlet::self()->removeDockWidget(this);
232 }
void setToolbarLayoutUpdated(Host *, TToolBar *)
Definition: mudlet.cpp:1472
QGridLayout * mpLayout
Definition: TToolBar.h:58
bool mVerticalOrientation
Definition: TToolBar.h:54
void slot_topLevelChanged(bool)
Definition: TToolBar.cpp:94
bool mUseCustomLayout
Definition: TAction.h:128
QString css
Definition: TAction.h:129
bool mIsPushDownButton
Definition: TAction.h:116
void setOrientation(Qt::Orientation)
Definition: TFlipButton.cpp:52
void finalize()
Definition: TToolBar.cpp:160
int mSizeY
Definition: TAction.h:126
void resizeEvent(QResizeEvent *e) override
Definition: TToolBar.cpp:64
TToolBar(TAction *, const QString &, QWidget *pW=nullptr)
Definition: TToolBar.cpp:33
bool getButtonFlat()
Definition: TAction.h:63
TAction * mpTAction
Definition: TFlipButton.h:54
int mPosX
Definition: TAction.h:107
void clear()
Definition: TToolBar.cpp:210
int mSizeX
Definition: TAction.h:125
bool mRecordMove
Definition: TToolBar.h:57
bool mButtonState
Definition: TAction.h:106
void slot_pressed(bool)
Definition: TToolBar.cpp:182
void setMirrored(bool)
Definition: TFlipButton.cpp:71
int mItemCount
Definition: TToolBar.h:59
TAction * mpTAction
Definition: TToolBar.h:49
QWidget * mpWidget
Definition: TToolBar.h:55
static mudlet * self()
Definition: mudlet.cpp:126
Qt::DockWidgetArea mToolbarLastDockArea
Definition: TAction.h:134
void moveEvent(QMoveEvent *e) override
Definition: TToolBar.cpp:71
bool mToolbarLastFloatingState
Definition: TAction.h:135
int getButtonColumns()
Definition: TAction.h:62
return false
Definition: ctelnet.cpp:465
void execute()
Definition: TAction.cpp:177
int mPosY
Definition: TAction.h:108
QPointer< Host > mpHost
Definition: TAction.h:131
QSize minimumSizeHint() const override
Definition: TFlipButton.cpp:85
int getButtonRotation()
Definition: TAction.h:60
void addButton(TFlipButton *pW)
Definition: TToolBar.cpp:101
void slot_dockLocationChanged(Qt::DockWidgetArea)
Definition: TToolBar.cpp:87