| 1 |
// SPDX-License-Identifier: LGPL-3.0-only |
| 2 |
/*************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright: https://code.google.com/p/qsint/ * |
| 5 |
* License: LGPL * |
| 6 |
* * |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
#include "actionbox.h" |
| 10 |
|
| 11 |
#include <QVariant> |
| 12 |
#include <QStyleOptionFrame> |
| 13 |
|
| 14 |
namespace QSint |
| 15 |
{ |
| 16 |
|
| 17 |
ActionBox::ActionBox(QWidget *parent) |
| 18 |
: QFrame(parent) |
| 19 |
{ |
| 20 |
init(); |
| 21 |
} |
| 22 |
|
| 23 |
ActionBox::ActionBox(const QString & headerText, QWidget *parent) |
| 24 |
: QFrame(parent) |
| 25 |
{ |
| 26 |
init(headerText); |
| 27 |
} |
| 28 |
|
| 29 |
ActionBox::ActionBox(const QPixmap & icon, const QString & headerText, QWidget *parent) |
| 30 |
: QFrame(parent) |
| 31 |
{ |
| 32 |
init(headerText); |
| 33 |
setIcon(icon); |
| 34 |
} |
| 35 |
|
| 36 |
void ActionBox::init(const QString &headerText) |
| 37 |
{ |
| 38 |
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 39 |
setFrameStyle(QFrame::NoFrame); |
| 40 |
|
| 41 |
auto *mainLayout = new QHBoxLayout(this); |
| 42 |
|
| 43 |
auto *iconLayout = new QVBoxLayout(); |
| 44 |
mainLayout->addLayout(iconLayout); |
| 45 |
|
| 46 |
iconLabel = new QLabel(this); |
| 47 |
iconLayout->addWidget(iconLabel); |
| 48 |
iconLayout->addStretch(); |
| 49 |
|
| 50 |
dataLayout = new QVBoxLayout(); |
| 51 |
mainLayout->addLayout(dataLayout); |
| 52 |
|
| 53 |
headerLabel = createItem(headerText); |
| 54 |
headerLabel->setProperty("class", "header"); |
| 55 |
} |
| 56 |
|
| 57 |
void ActionBox::setIcon(const QPixmap & icon) |
| 58 |
{ |
| 59 |
iconLabel->setPixmap(icon); |
| 60 |
iconLabel->setFixedSize(icon.size()); |
| 61 |
} |
| 62 |
|
| 63 |
QPixmap ActionBox::icon() const |
| 64 |
{ |
| 65 |
return iconLabel->pixmap(Qt::ReturnByValue); |
| 66 |
} |
| 67 |
|
| 68 |
ActionLabel* ActionBox::createItem(QAction * action, QLayout * l) |
| 69 |
{ |
| 70 |
if (!action) { |
| 71 |
return nullptr; |
| 72 |
} |
| 73 |
ActionLabel *act = createItem("", l); |
| 74 |
act->setDefaultAction(action); |
| 75 |
return act; |
| 76 |
} |
| 77 |
|
| 78 |
QList<ActionLabel*> ActionBox::createItems(QList<QAction*> actions) |
| 79 |
{ |
| 80 |
QList<ActionLabel*> list; |
| 81 |
|
| 82 |
if (actions.isEmpty()) { |
| 83 |
return list; |
| 84 |
} |
| 85 |
QLayout *l = createHBoxLayout(); |
| 86 |
|
| 87 |
for (QAction *action : actions) { |
| 88 |
if (auto *act = createItem(action, l)) { |
| 89 |
list.append(act); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return list; |
| 94 |
} |
| 95 |
|
| 96 |
ActionLabel* ActionBox::createItem(const QString & text, QLayout * l) |
| 97 |
{ |
| 98 |
ActionLabel *act = new ActionLabel(this); |
| 99 |
act->setText(text); |
| 100 |
act->setProperty("class", "action"); |
| 101 |
|
| 102 |
if (l) { |
| 103 |
l->addWidget(act); |
| 104 |
} |
| 105 |
else { |
| 106 |
QHBoxLayout *hbl = new QHBoxLayout(); |
| 107 |
hbl->addWidget(act); |
| 108 |
createSpacer(hbl); |
| 109 |
dataLayout->addLayout(hbl); |
| 110 |
} |
| 111 |
|
| 112 |
return act; |
| 113 |
} |
| 114 |
|
| 115 |
ActionLabel* ActionBox::createItem(const QPixmap & icon, const QString & text, QLayout * l) |
| 116 |
{ |
| 117 |
ActionLabel *act = createItem(text, l); |
| 118 |
act->setIcon(QIcon(icon)); |
| 119 |
return act; |
| 120 |
} |
| 121 |
|
| 122 |
QSpacerItem* ActionBox::createSpacer(QLayout * l) |
| 123 |
{ |
| 124 |
QSpacerItem * spacer; |
| 125 |
|
| 126 |
if (l) { |
| 127 |
// add horizontal spacer |
| 128 |
l->addItem(spacer = new QSpacerItem(1,0,QSizePolicy::MinimumExpanding,QSizePolicy::Ignored)); |
| 129 |
} |
| 130 |
else { |
| 131 |
// add vertical spacer |
| 132 |
dataLayout->addItem(spacer = new QSpacerItem(0,1,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding)); |
| 133 |
} |
| 134 |
return spacer; |
| 135 |
} |
| 136 |
|
| 137 |
QLayout* ActionBox::createHBoxLayout() |
| 138 |
{ |
| 139 |
QHBoxLayout *hbl = new QHBoxLayout(); |
| 140 |
dataLayout->addLayout(hbl); |
| 141 |
QHBoxLayout *hbl1 = new QHBoxLayout(); |
| 142 |
hbl->addLayout(hbl1); |
| 143 |
createSpacer(hbl); |
| 144 |
return hbl1; |
| 145 |
} |
| 146 |
|
| 147 |
void ActionBox::addLayout(QLayout * l) |
| 148 |
{ |
| 149 |
if (l) { |
| 150 |
dataLayout->addLayout(l); |
| 151 |
l->setParent(this); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void ActionBox::addWidget(QWidget * w, QLayout * l) |
| 156 |
{ |
| 157 |
if (!w) { |
| 158 |
return; |
| 159 |
} |
| 160 |
w->setParent(this); |
| 161 |
|
| 162 |
if (l) { |
| 163 |
l->addWidget(w); |
| 164 |
} |
| 165 |
else { |
| 166 |
QHBoxLayout *hbl = new QHBoxLayout(); |
| 167 |
hbl->addWidget(w); |
| 168 |
createSpacer(hbl); |
| 169 |
dataLayout->addLayout(hbl); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
QSize ActionBox::minimumSizeHint() const |
| 174 |
{ |
| 175 |
return {150,65}; |
| 176 |
} |
| 177 |
|
| 178 |
} // namespace |