| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 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 |
#include <QEvent> |
| 25 |
#include <QGridLayout> |
| 26 |
#include <QToolBox> |
| 27 |
|
| 28 |
|
| 29 |
#include "ToolBox.h" |
| 30 |
|
| 31 |
|
| 32 |
using namespace Gui::DockWnd; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructs a toolbox called \a name with parent \a parent and flags \a f. |
| 36 |
*/ |
| 37 |
ToolBox::ToolBox(QWidget* parent) |
| 38 |
: QWidget(parent) |
| 39 |
{ |
| 40 |
_pToolBox = new QToolBox(this); |
| 41 |
connect(_pToolBox, &QToolBox::currentChanged, this, &ToolBox::currentChanged); |
| 42 |
|
| 43 |
auto pGrid = new QGridLayout(this); |
| 44 |
pGrid->addWidget(_pToolBox, 0, 0); |
| 45 |
} |
| 46 |
|
| 47 |
ToolBox::~ToolBox() |
| 48 |
{ |
| 49 |
delete _pToolBox; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Adds the widget w in a new tab at bottom of the toolbox. The new tab's label is set to \a label. |
| 54 |
* Returns the new tab's index. |
| 55 |
*/ |
| 56 |
int ToolBox::addItem(QWidget* w, const QString& label) |
| 57 |
{ |
| 58 |
return _pToolBox->addItem(w, label); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Adds the widget \a item in a new tab at bottom of the toolbox. The new tab's label is set to \a |
| 63 |
* label, and the \a iconSet is displayed to the left of the \a label. Returns the new tab's index. |
| 64 |
*/ |
| 65 |
int ToolBox::addItem(QWidget* item, const QIcon& iconSet, const QString& label) |
| 66 |
{ |
| 67 |
return _pToolBox->addItem(item, iconSet, label); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* This is an overloaded member function, provided for convenience. It behaves essentially like the |
| 72 |
* above function. |
| 73 |
* |
| 74 |
* Inserts the widget \a item at position \a index, or at the bottom of the toolbox if \a index is |
| 75 |
* out of range. The new item's label is set to \a label. Returns the new item's index. |
| 76 |
*/ |
| 77 |
int ToolBox::insertItem(int index, QWidget* item, const QString& label) |
| 78 |
{ |
| 79 |
return _pToolBox->insertItem(index, item, label); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Inserts the widget \a item at position \a index, or at the bottom of the toolbox if \a index is |
| 84 |
* out of range. The new item's label is set to \a label, and the \a iconSet is displayed to the |
| 85 |
* left of the \a label. Returns the new item's index. |
| 86 |
*/ |
| 87 |
int ToolBox::insertItem(int index, QWidget* item, const QIcon& iconSet, const QString& label) |
| 88 |
{ |
| 89 |
return _pToolBox->insertItem(index, item, iconSet, label); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Removes the widget \a item from the toolbox. |
| 94 |
* Returns the removed widget's index, or -1 if the widget was not in this tool box. |
| 95 |
*/ |
| 96 |
void ToolBox::removeItem(int index) |
| 97 |
{ |
| 98 |
_pToolBox->removeItem(index); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* If \a enabled is true then the item at position \a index is enabled; otherwise item \a index is |
| 103 |
* disabled. |
| 104 |
*/ |
| 105 |
void ToolBox::setItemEnabled(int index, bool enabled) |
| 106 |
{ |
| 107 |
_pToolBox->setItemEnabled(index, enabled); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns true if the item at position \a index is enabled; otherwise returns false. |
| 112 |
*/ |
| 113 |
bool ToolBox::isItemEnabled(int index) const |
| 114 |
{ |
| 115 |
return _pToolBox->isItemEnabled(index); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Sets the label of the item at position \a index to \a label. |
| 120 |
*/ |
| 121 |
void ToolBox::setItemText(int index, const QString& label) |
| 122 |
{ |
| 123 |
_pToolBox->setItemText(index, label); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the label of the item at position \a index, or a null string if \a index is out of range. |
| 128 |
*/ |
| 129 |
QString ToolBox::itemText(int index) const |
| 130 |
{ |
| 131 |
return _pToolBox->itemText(index); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Sets the icon of the item at position \a index to \a iconSet. |
| 136 |
*/ |
| 137 |
void ToolBox::setItemIcon(int index, const QIcon& iconSet) |
| 138 |
{ |
| 139 |
_pToolBox->setItemIcon(index, iconSet); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the icon of the item at position \a index, or a null icon if \a index is out of range. |
| 144 |
*/ |
| 145 |
QIcon ToolBox::itemIcon(int index) const |
| 146 |
{ |
| 147 |
return _pToolBox->itemIcon(index); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Sets the tooltip of the item at position \a index to \a toolTip. |
| 152 |
*/ |
| 153 |
void ToolBox::setItemToolTip(int index, const QString& toolTip) |
| 154 |
{ |
| 155 |
_pToolBox->setItemToolTip(index, toolTip); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the tooltip of the item at position \a index, or a null string if \a index is out of range. |
| 160 |
*/ |
| 161 |
QString ToolBox::itemToolTip(int index) const |
| 162 |
{ |
| 163 |
return _pToolBox->itemToolTip(index); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns the toolbox's current item, or 0 if the toolbox is empty. |
| 168 |
*/ |
| 169 |
QWidget* ToolBox::currentWidget() const |
| 170 |
{ |
| 171 |
return _pToolBox->currentWidget(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Sets the current item to be \a item. |
| 176 |
*/ |
| 177 |
void ToolBox::setCurrentWidget(QWidget* item) |
| 178 |
{ |
| 179 |
_pToolBox->setCurrentWidget(item); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns the index of the current item, or -1 if the toolbox is empty. |
| 184 |
*/ |
| 185 |
int ToolBox::currentIndex() const |
| 186 |
{ |
| 187 |
return _pToolBox->currentIndex(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns the item at position \a index, or 0 if there is no such item. |
| 192 |
*/ |
| 193 |
QWidget* ToolBox::widget(int index) const |
| 194 |
{ |
| 195 |
return _pToolBox->widget(index); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the index of item \a item, or -1 if the item does not exist. |
| 200 |
*/ |
| 201 |
int ToolBox::indexOf(QWidget* item) const |
| 202 |
{ |
| 203 |
return _pToolBox->indexOf(item); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the number of items contained in the toolbox. |
| 208 |
*/ |
| 209 |
int ToolBox::count() const |
| 210 |
{ |
| 211 |
return _pToolBox->count(); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Sets the index of the current item, or -1 if the toolbox is empty to \a index. |
| 216 |
*/ |
| 217 |
void ToolBox::setCurrentIndex(int index) |
| 218 |
{ |
| 219 |
_pToolBox->setCurrentIndex(index); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* If to a new language is switched this method gets called. |
| 224 |
*/ |
| 225 |
void ToolBox::changeEvent(QEvent* e) |
| 226 |
{ |
| 227 |
if (e->type() == QEvent::LanguageChange) { |
| 228 |
QWidget::changeEvent(e); |
| 229 |
int ct = count(); |
| 230 |
for (int i = 0; i < ct; i++) { |
| 231 |
QWidget* w = widget(i); |
| 232 |
if (w) { |
| 233 |
setItemText(i, w->windowTitle()); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
else { |
| 238 |
QWidget::changeEvent(e); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
#include "moc_ToolBox.cpp" |