1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* toolbar.cpp - plugin
* Copyright (C) 2009-2011 Evgeny Khryukin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <QAction>
#include <QLabel>
#include <QSpinBox>
#include "screenshoticonset.h"
#include "toolbar.h"
class Button : public QAction {
Q_OBJECT
public:
Button(const QString &tip, const QIcon &ico, ToolBar::ButtonType type, bool checkable, QWidget *parent) :
QAction(parent), type_(type)
{
setToolTip(tip);
setIcon(ico);
setCheckable(checkable);
}
ToolBar::ButtonType type() const { return type_; }
private:
ToolBar::ButtonType type_;
};
//----------------ToolBar--------------------------------
ToolBar::ToolBar(QWidget *parent) : QToolBar(parent) { init(); }
ToolBar::~ToolBar()
{
for (Button *b : buttons_) {
delete (b);
}
buttons_.clear();
}
void ToolBar::init()
{
ScreenshotIconset *icoHost = ScreenshotIconset::instance();
addWidget(new QLabel(tr("Line Width:")));
sb = new QSpinBox(this);
sb->setMinimum(1);
setLineWidth(2);
sb->setToolTip(tr("Line width"));
addWidget(sb);
// TODO: update after stopping support of Ubuntu Xenial:
connect(sb, SIGNAL(valueChanged(int)), this, SIGNAL(newWidth(int)));
QPixmap pix(16, 16);
pix.fill(QColor(Qt::black));
QIcon ico(pix);
buttons_.append(new Button(tr("Select Color"), ico, ToolBar::ButtonColor, false, this));
buttons_.append(new Button(tr("Pen"), icoHost->getIcon("psi/draw"), ToolBar::ButtonPen, true, this));
buttons_.append(new Button(tr("Select"), icoHost->getIcon("psi/frame"), ToolBar::ButtonSelect, true, this));
buttons_.append(new Button(tr("Cut"), icoHost->getIcon("psi/crop"), ToolBar::ButtonCut, false, this));
buttons_.last()->setShortcut(QKeySequence("Ctrl+x"));
buttons_.append(new Button(tr("Copy"), icoHost->getIcon("psi/copy"), ToolBar::ButtonCopy, false, this));
buttons_.last()->setShortcut(QKeySequence("Ctrl+c"));
buttons_.append(new Button(tr("Paste"), icoHost->getIcon("psi/paste"), ToolBar::ButtonInsert, false, this));
buttons_.last()->setShortcut(QKeySequence("Ctrl+v"));
buttons_.append(new Button(tr("Rotate"), icoHost->getIcon("psi/rotate"), ToolBar::ButtonRotate, false, this));
buttons_.append(new Button(tr("Insert Text"), icoHost->getIcon("psi/text"), ToolBar::ButtonText, true, this));
buttons_.append(new Button(tr("Undo"), icoHost->getIcon("psi/undo"), ToolBar::ButtonUndo, false, this));
buttons_.last()->setShortcut(QKeySequence("Ctrl+z"));
for (Button *b : buttons_) {
addAction(b);
// TODO: update after stopping support of Ubuntu Xenial:
connect(b, SIGNAL(triggered(bool)), SLOT(buttonChecked(bool)));
connect(b, SIGNAL(triggered()), SLOT(buttonClicked()));
}
enableButton(false, ToolBar::ButtonUndo);
}
void ToolBar::enableButton(bool enable, ToolBar::ButtonType type)
{
for (Button *b : buttons_) {
if (b->type() == type) {
b->setEnabled(enable);
break;
}
}
}
void ToolBar::checkButton(ToolBar::ButtonType type)
{
for (Button *b : buttons_) {
if (b->type() == type && b->isCheckable()) {
b->setChecked(true);
break;
}
}
emit checkedButtonChanged(type);
}
void ToolBar::buttonChecked(bool check)
{
Button *s = (Button *)sender();
if (!s->isCheckable()) {
return;
}
if (s->type() == ButtonSelect && check) {
enableButton(true, ButtonCut);
} else {
enableButton(false, ButtonCut);
}
if (check) {
for (Button *b : buttons_) {
if (b != s)
b->setChecked(false);
}
emit checkedButtonChanged(s->type());
} else
emit checkedButtonChanged(ToolBar::ButtonNoButton);
}
void ToolBar::setColorForColorButton(const QColor &color)
{
for (Button *b : buttons_) {
if (b->type() == ButtonColor) {
QPixmap pix(16, 16);
pix.fill(color);
b->setIcon(QIcon(pix));
break;
}
}
}
void ToolBar::buttonClicked()
{
Button *s = (Button *)sender();
if (s)
emit buttonClicked(s->type());
}
ToolBar::ButtonType ToolBar::currentButton() const
{
for (Button *b : buttons_) {
if (b->isChecked())
return b->type();
}
return ToolBar::ButtonNoButton;
}
void ToolBar::setLineWidth(int width) { sb->setValue(width); }
#include "toolbar.moc"
|