libplasma
toolbox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "toolbox_p.h"
00023
00024 #include <QAction>
00025 #include <QGraphicsSceneHoverEvent>
00026 #include <QPainter>
00027 #include <QRadialGradient>
00028
00029 #include <KColorScheme>
00030 #include <KDebug>
00031
00032 #include <plasma/theme.h>
00033 #include "widgets/icon.h"
00034
00035 namespace Plasma
00036 {
00037
00038 class ToolBoxPrivate
00039 {
00040 public:
00041 ToolBoxPrivate()
00042 : size(50),
00043 iconSize(32, 32),
00044 hidden(false),
00045 showing(false),
00046 orientation(Qt::Horizontal)
00047 {}
00048
00049 int size;
00050 QSize iconSize;
00051 bool hidden;
00052 bool showing;
00053 Qt::Orientation orientation;
00054 };
00055
00056 ToolBox::ToolBox(QGraphicsItem *parent)
00057 : QGraphicsItem(parent),
00058 d(new ToolBoxPrivate)
00059 {
00060 setAcceptsHoverEvents(true);
00061 }
00062
00063 ToolBox::~ToolBox()
00064 {
00065 delete d;
00066 }
00067
00068 void ToolBox::addTool(QAction *action)
00069 {
00070 if (!action) {
00071 return;
00072 }
00073
00074 Plasma::Icon *tool = new Plasma::Icon(this);
00075
00076 tool->setAction(action);
00077 tool->setDrawBackground(true);
00078 tool->setOrientation(Qt::Horizontal);
00079 QSizeF iconSize = tool->sizeFromIconSize(22);
00080 tool->setMinimumSize(iconSize);
00081 tool->setMaximumSize(iconSize);
00082 tool->resize(tool->size());
00083
00084 tool->hide();
00085 const int height = static_cast<int>(tool->boundingRect().height());
00086 tool->setPos(QPoint( d->size*2,-height));
00087 tool->setZValue(zValue() + 1);
00088
00089
00090 connect(tool, SIGNAL(changed()), this, SLOT(updateToolBox()));
00091 }
00092
00093 void ToolBox::updateToolBox()
00094 {
00095 if ( d->showing) {
00096 d->showing = false;
00097 showToolBox();
00098 }
00099 }
00100
00101 void ToolBox::removeTool(QAction *action)
00102 {
00103 foreach (QGraphicsItem *child, QGraphicsItem::children()) {
00104
00105 Plasma::Icon *tool = dynamic_cast<Plasma::Icon*>(child);
00106 if (tool && tool->action() == action) {
00107
00108 tool->deleteLater();
00109 break;
00110 }
00111 }
00112 }
00113
00114 int ToolBox::size() const
00115 {
00116 return d->size;
00117 }
00118
00119 void ToolBox::setSize(const int newSize)
00120 {
00121 d->size = newSize;
00122 }
00123
00124 QSize ToolBox::iconSize() const
00125 {
00126 return d->iconSize;
00127 }
00128
00129 void ToolBox::setIconSize(const QSize newSize)
00130 {
00131 d->iconSize = newSize;
00132 }
00133
00134 bool ToolBox::showing() const
00135 {
00136 return d->showing;
00137 }
00138
00139 void ToolBox::setShowing(const bool show)
00140 {
00141 d->showing = show;
00142 }
00143
00144 Qt::Orientation ToolBox::orientation() const
00145 {
00146 return d->orientation;
00147 }
00148
00149 void ToolBox::setOrientation( Qt::Orientation orient )
00150 {
00151 d->orientation = orient;
00152 }
00153
00154 void ToolBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
00155 {
00156 event->accept();
00157 }
00158
00159 void ToolBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
00160 {
00161 if (boundingRect().contains(event->pos())) {
00162 emit toggled();
00163 }
00164 }
00165
00166 }
00167
00168 #include "toolbox_p.moc"
00169