move the IconWidget version of the tools into the desktop tool box; it is what uses them and this prevents having to keep fixing the "tools are showing in the panel" bug every time something in the toolboxes and/or panel containments and/or panel views changes ;)

svn path=/trunk/KDE/kdelibs/; revision=1130635
This commit is contained in:
Aaron J. Seigo 2010-05-25 21:07:09 +00:00
parent 30b2a10d57
commit 6a286f380e
4 changed files with 105 additions and 96 deletions

View File

@ -123,7 +123,7 @@ class EmptyGraphicsItem : public QGraphicsWidget
event->accept(); event->accept();
} }
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{ {
m_itemBackground->hide(); m_itemBackground->hide();
} }
@ -195,6 +195,7 @@ public:
QColor fgColor; QColor fgColor;
QColor bgColor; QColor bgColor;
bool hovering : 1; bool hovering : 1;
QMultiMap<AbstractToolBox::ToolType, IconWidget *> tools;
}; };
DesktopToolBox::DesktopToolBox(Containment *parent) DesktopToolBox::DesktopToolBox(Containment *parent)
@ -537,12 +538,82 @@ void DesktopToolBox::showToolBox()
fadeAnim->start(QAbstractAnimation::DeleteWhenStopped); fadeAnim->start(QAbstractAnimation::DeleteWhenStopped);
} }
void DesktopToolBox::addTool(QAction *action)
{
InternalToolBox::addTool(action);
Plasma::IconWidget *tool = new Plasma::IconWidget(toolParent());
tool->setTextBackgroundColor(QColor());
tool->setAction(action);
tool->setDrawBackground(true);
tool->setOrientation(Qt::Horizontal);
tool->resize(tool->sizeFromIconSize(KIconLoader::SizeSmallMedium));
tool->setPreferredIconSize(QSizeF(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium));
tool->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
tool->hide();
const int height = static_cast<int>(tool->boundingRect().height());
tool->setPos(toolPosition(height));
tool->setZValue(zValue() + 10);
tool->setToolTip(action->text());
//make enabled/disabled tools appear/disappear instantly
connect(tool, SIGNAL(changed()), this, SLOT(updateToolBox()));
ToolType type = AbstractToolBox::MiscTool;
if (!action->data().isNull() && action->data().type() == QVariant::Int) {
int t = action->data().toInt();
if (t >= 0 && t < AbstractToolBox::UserToolType) {
type = static_cast<AbstractToolBox::ToolType>(t);
}
}
d->tools.insert(type, tool);
//kDebug() << "added tool" << type << action->text();
}
void DesktopToolBox::updateToolBox() void DesktopToolBox::updateToolBox()
{ {
InternalToolBox::updateToolBox(); Plasma::IconWidget *tool = qobject_cast<Plasma::IconWidget *>(sender());
if (tool && !tool->action()) {
QMutableMapIterator<ToolType, IconWidget *> it(d->tools);
while (it.hasNext()) {
it.next();
if (it.value() == tool) {
it.remove();
break;
}
}
tool->deleteLater();
tool = 0;
}
if (isShowing()) {
showToolBox();
} else if (tool && !tool->isEnabled()) {
tool->hide();
}
adjustToolBackerGeometry(); adjustToolBackerGeometry();
} }
void DesktopToolBox::removeTool(QAction *action)
{
QMutableMapIterator<ToolType, IconWidget *> it(d->tools);
while (it.hasNext()) {
it.next();
IconWidget *tool = it.value();
//kDebug() << "checking tool" << tool
if (tool && tool->action() == action) {
//kDebug() << "tool found!";
tool->deleteLater();
it.remove();
break;
}
}
}
void DesktopToolBox::adjustToolBackerGeometry() void DesktopToolBox::adjustToolBackerGeometry()
{ {
if (!d->toolBacker) { if (!d->toolBacker) {
@ -550,8 +621,7 @@ void DesktopToolBox::adjustToolBackerGeometry()
} }
d->toolBacker->clearLayout(); d->toolBacker->clearLayout();
QMap<ToolType, IconWidget *> t = tools(); QMapIterator<ToolType, IconWidget *> it(d->tools);
QMapIterator<ToolType, IconWidget *> it(t);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
IconWidget *icon = it.value(); IconWidget *icon = it.value();

View File

@ -49,6 +49,8 @@ public:
QRectF boundingRect() const; QRectF boundingRect() const;
QPainterPath shape() const; QPainterPath shape() const;
void addTool(QAction *action);
void removeTool(QAction *action);
void showToolBox(); void showToolBox();
void hideToolBox(); void hideToolBox();

View File

@ -51,7 +51,8 @@ public:
showing(false), showing(false),
movable(false), movable(false),
dragging(false), dragging(false),
userMoved(false) userMoved(false),
iconic(true)
{} {}
Containment *containment; Containment *containment;
@ -60,12 +61,13 @@ public:
QSize iconSize; QSize iconSize;
QPoint dragStartRelative; QPoint dragStartRelative;
QTransform viewTransform; QTransform viewTransform;
QMultiMap<AbstractToolBox::ToolType, IconWidget *> tools; QList<QAction *> actions;
bool hidden : 1; bool hidden : 1;
bool showing : 1; bool showing : 1;
bool movable : 1; bool movable : 1;
bool dragging : 1; bool dragging : 1;
bool userMoved : 1; bool userMoved : 1;
bool iconic : 1;
}; };
InternalToolBox::InternalToolBox(Containment *parent) InternalToolBox::InternalToolBox(Containment *parent)
@ -109,11 +111,6 @@ QPoint InternalToolBox::toolPosition(int toolHeight)
} }
} }
QMap<AbstractToolBox::ToolType, IconWidget *> InternalToolBox::tools() const
{
return d->tools;
}
QGraphicsWidget *InternalToolBox::toolParent() QGraphicsWidget *InternalToolBox::toolParent()
{ {
return this; return this;
@ -125,92 +122,33 @@ void InternalToolBox::addTool(QAction *action)
return; return;
} }
foreach (IconWidget *tool, d->tools) { if (d->actions.contains(action)) {
//kDebug() << "checking tool" << child << child->data(ToolName); return;
if (tool->action() == action) {
return;
}
} }
Plasma::IconWidget *tool = new Plasma::IconWidget(toolParent()); connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
tool->setTextBackgroundColor(QColor());
tool->setAction(action);
tool->setDrawBackground(true);
tool->setOrientation(Qt::Horizontal);
tool->resize(tool->sizeFromIconSize(KIconLoader::SizeSmallMedium));
tool->setPreferredIconSize(QSizeF(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium));
tool->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
tool->hide();
const int height = static_cast<int>(tool->boundingRect().height());
tool->setPos(toolPosition(height));
tool->setZValue(zValue() + 10);
tool->setToolTip(action->text());
//make enabled/disabled tools appear/disappear instantly
connect(tool, SIGNAL(changed()), this, SLOT(updateToolBox()));
connect(action, SIGNAL(triggered(bool)), this, SLOT(toolTriggered(bool))); connect(action, SIGNAL(triggered(bool)), this, SLOT(toolTriggered(bool)));
d->actions.append(action);
ToolType type = AbstractToolBox::MiscTool;
if (!action->data().isNull() && action->data().type() == QVariant::Int) {
int t = action->data().toInt();
if (t >= 0 && t < AbstractToolBox::UserToolType) {
type = static_cast<AbstractToolBox::ToolType>(t);
}
}
d->tools.insert(type, tool);
//kDebug() << "added tool" << type << action->text();
}
bool InternalToolBox::isEmpty() const
{
return d->tools.isEmpty();
}
void InternalToolBox::updateToolBox()
{
Plasma::IconWidget *tool = qobject_cast<Plasma::IconWidget *>(sender());
if (tool && !tool->action()) {
QMutableMapIterator<ToolType, IconWidget *> it(d->tools);
while (it.hasNext()) {
it.next();
if (it.value() == tool) {
it.remove();
break;
}
}
tool->deleteLater();
tool = 0;
}
if (d->showing) {
showToolBox();
} else if (tool && !tool->isEnabled()) {
tool->hide();
}
}
void InternalToolBox::toolTriggered(bool)
{
} }
void InternalToolBox::removeTool(QAction *action) void InternalToolBox::removeTool(QAction *action)
{ {
QMutableMapIterator<ToolType, IconWidget *> it(d->tools); disconnect(action, 0, this, 0);
while (it.hasNext()) { d->actions.removeAll(action);
it.next(); }
IconWidget *tool = it.value();
//kDebug() << "checking tool" << tool void InternalToolBox::actionDestroyed(QObject *object)
if (tool && tool->action() == action) { {
//kDebug() << "tool found!"; d->actions.removeAll(static_cast<QAction*>(object));
tool->deleteLater(); }
it.remove();
break; bool InternalToolBox::isEmpty() const
} {
} return d->actions.isEmpty();
}
void InternalToolBox::toolTriggered(bool)
{
} }
int InternalToolBox::size() const int InternalToolBox::size() const

View File

@ -87,19 +87,17 @@ public:
virtual QSize fullWidth() const; virtual QSize fullWidth() const;
virtual QSize fullHeight() const; virtual QSize fullHeight() const;
virtual QSize cornerSize() const; virtual QSize cornerSize() const;
virtual void updateToolBox() {}
void setIconic(bool iconic);
bool iconic() const;
virtual void showToolBox() = 0; virtual void showToolBox() = 0;
virtual void hideToolBox() = 0; virtual void hideToolBox() = 0;
public Q_SLOTS:
/**
* re-show the toolbox, in case any tools have changed
*/
virtual void updateToolBox();
protected: protected:
Containment *containment(); Containment *containment();
QPoint toolPosition(int toolHeight); QPoint toolPosition(int toolHeight);
QMap<AbstractToolBox::ToolType, IconWidget *> tools() const;
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
@ -107,6 +105,7 @@ protected:
protected Q_SLOTS: protected Q_SLOTS:
virtual void toolTriggered(bool); virtual void toolTriggered(bool);
void actionDestroyed(QObject *object);
private: private:
InternalToolBoxPrivate *d; InternalToolBoxPrivate *d;