Mudlet  0
Mudclient
TEasyButtonBar.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 "TEasyButtonBar.h"
24 
25 #include "Host.h"
26 #include "TAction.h"
27 #include "TConsole.h"
28 #include "TFlipButton.h"
29 
30 #include "pre_guard.h"
31 #include <QGridLayout>
32 #include "post_guard.h"
33 
34 
36 : QWidget( pW )
37 , mpTAction( pA )
38 , mVerticalOrientation( false )
39 , mpWidget( new QWidget )
40 , mName( name )
41 , mRecordMove( false )
42 , mpLayout( nullptr )
43 , mItemCount( 0 )
44 , mpBar( pW )
45 {
46  mButtonList.clear();
47  auto layout = new QVBoxLayout;
48  setLayout(layout);
49  layout->setContentsMargins(0, 0, 0, 0);
50  layout->setMargin(0);
51  layout->setSpacing(0);
52  layout->addWidget(mpWidget);
54  mpLayout = new QGridLayout(mpWidget);
55  setContentsMargins(0, 0, 0, 0);
56  mpLayout->setContentsMargins(0, 0, 0, 0);
57  mpLayout->setMargin(0);
58  mpLayout->setSpacing(0);
59  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
60  mpWidget->setSizePolicy(sizePolicy);
61  } else {
62  mpWidget->setMinimumHeight(mpTAction->mSizeY);
63  mpWidget->setMaximumHeight(mpTAction->mSizeY);
64  mpWidget->setMinimumWidth(mpTAction->mSizeX);
65  mpWidget->setMaximumWidth(mpTAction->mSizeX);
67  }
68  setStyleSheet(mpTAction->css);
69  mpWidget->setStyleSheet(mpTAction->css);
70 }
71 
73 {
75  QSize size = pB->minimumSizeHint();
76  if (pB->mpTAction->getButtonRotation() > 0) {
77  size.transpose();
78  pB->setMaximumSize(size);
79  }
80  } else {
81  qDebug() << "setting up custom sizes";
82  QSize size = QSize(pB->mpTAction->mSizeX, pB->mpTAction->mSizeY);
83  pB->setMaximumSize(size);
84  pB->setMinimumSize(size);
85  pB->setParent(mpWidget);
86  pB->setGeometry(pB->mpTAction->mPosX, pB->mpTAction->mPosY, pB->mpTAction->mSizeX, pB->mpTAction->mSizeY);
87  }
88 
89  pB->setStyleSheet(pB->mpTAction->css);
90  pB->setFlat(pB->mpTAction->getButtonFlat());
91  int rotation = pB->mpTAction->getButtonRotation();
92  switch (rotation) {
93  case 0:
94  pB->setOrientation(Qt::Horizontal);
95  break;
96  case 1:
97  pB->setOrientation(Qt::Vertical);
98  break;
99  case 2:
100  pB->setOrientation(Qt::Vertical);
101  pB->setMirrored(true);
102  break;
103  }
104 
105  if (!mpTAction->mUseCustomLayout) {
106  // tool bar mButtonColumns > 0 -> autolayout
107  // case == 0: use individual button placement for user defined layouts
108  int columns = mpTAction->getButtonColumns();
109  if (columns <= 0) {
110  columns = 1;
111  }
112  if (columns > 0) {
113  mItemCount++;
114  int row = mItemCount / columns;
115  int col = mItemCount % columns;
116  if (mVerticalOrientation) {
117  mpLayout->addWidget(pB, row, col);
118  } else {
119  mpLayout->addWidget(pB, col, row);
120  }
121  }
122  } else {
123  pB->move(pB->mpTAction->mPosX, pB->mpTAction->mPosY);
124  }
125 
126 
127  // Was using released() signal but now we want to track the ACTUAL state of
128  // the underlying QAbstractButton
129  connect(pB, &QAbstractButton::clicked, this, &TEasyButtonBar::slot_pressed);
130  mButtonList.push_back(pB);
131  pB->setChecked(pB->mpTAction->mButtonState);
132 }
133 
134 
136 {
138  return;
139  }
140  auto fillerWidget = new QWidget;
141 
142  QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
143  fillerWidget->setSizePolicy(sizePolicy);
144  int columns = mpTAction->getButtonColumns();
145  if (columns <= 0) {
146  columns = 1;
147  }
148  int row = (++mItemCount) / columns;
149  int column = mItemCount % columns;
150  if (mpLayout) {
151  mpLayout->addWidget(fillerWidget, row, column);
152  }
153 }
154 
155 // Used by buttons directly on an TEasyButtonBar instance - we now retrieve the
156 // button state to ensure the visible representation is used.
157 void TEasyButtonBar::slot_pressed(const bool isChecked)
158 {
159  auto * pB = dynamic_cast<TFlipButton*>(sender());
160  if (!pB) {
161  return;
162  }
163 
164  TAction* pA = pB->mpTAction;
165 
166  // NOTE: This function blocks until an item is selected from the menu, and,
167  // as the action to "pop-up" the menu is the same as "buttons" use to
168  // perform their command/scripts is why "commands" are (no longer) permitted
169  // on a "menu". It also means that the script for a "menu" is run every
170  // time it is "clicked" upon to display the pop-up containing the menu
171  // entries...
172  pB->showMenu();
173 
174  if (pA->mIsPushDownButton) {
175  // DO NOT MANIPULATE THE BUTTON STATE OURSELF NOW
176  pA->mButtonState = isChecked;
177  pA->mpHost->mpConsole->mButtonState = (pA->mButtonState ? 2 : 1);
178  } else {
179  pA->mButtonState = false; // Forces a fixup if not correct
180  pB->setChecked(false); // This does NOT invoke the clicked() signal!
181  pA->mpHost->mpConsole->mButtonState = 1; // Was effectively 0 but that is wrong
182  }
183 
184  pA->execute();
185 }
186 
188 {
189  auto pW = new QWidget;
190  for (auto& flipButton : mButtonList) {
191  disconnect(flipButton, &QAbstractButton::clicked, this, &TEasyButtonBar::slot_pressed);
192  }
193  mButtonList.clear();
194  mpWidget->deleteLater();
195  mpWidget = pW;
196 
197  if (!mpTAction->mUseCustomLayout) {
198  mpLayout = new QGridLayout;
199  mpWidget->setLayout(mpLayout);
200  mpLayout->setContentsMargins(0, 0, 0, 0);
201  mpLayout->setSpacing(0);
202  mpLayout->setMargin(0);
203  QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
204  mpWidget->setSizePolicy(sizePolicy);
205 
206  mpWidget->setContentsMargins(0, 0, 0, 0);
207  mpLayout->setMargin(0);
208  } else {
209  mpLayout = nullptr;
210  mpWidget->setMinimumHeight(mpTAction->mSizeY);
211  mpWidget->setMaximumHeight(mpTAction->mSizeY);
212  mpWidget->setMinimumWidth(mpTAction->mSizeX);
213  mpWidget->setMaximumWidth(mpTAction->mSizeX);
215  }
216  layout()->addWidget(pW);
217  setStyleSheet(mpTAction->css);
218  mpWidget->setStyleSheet(mpTAction->css);
219 }
QWidget * mpWidget
TAction * mpTAction
bool mUseCustomLayout
Definition: TAction.h:128
TEasyButtonBar(TAction *, QString, QWidget *pW=nullptr)
QString css
Definition: TAction.h:129
bool mIsPushDownButton
Definition: TAction.h:116
void setOrientation(Qt::Orientation)
Definition: TFlipButton.cpp:52
int mSizeY
Definition: TAction.h:126
void addButton(TFlipButton *pW)
bool getButtonFlat()
Definition: TAction.h:63
TAction * mpTAction
Definition: TFlipButton.h:54
int mPosX
Definition: TAction.h:107
int mSizeX
Definition: TAction.h:125
bool mButtonState
Definition: TAction.h:106
bool mVerticalOrientation
void setMirrored(bool)
Definition: TFlipButton.cpp:71
QGridLayout * mpLayout
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
std::list< TFlipButton * > mButtonList
int getButtonRotation()
Definition: TAction.h:60
void slot_pressed(bool)