[Ktutorial-commits] SF.net SVN: ktutorial:[141] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-10 17:51:03
|
Revision: 141 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=141&view=rev Author: danxuliu Date: 2010-03-10 17:50:56 +0000 (Wed, 10 Mar 2010) Log Message: ----------- Add docks to show the edition actions. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.cpp trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.h trunk/ktutorial/ktutorial-editor/tests/unit/view/ActionListWidgetTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-09 21:12:53 UTC (rev 140) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-10 17:50:56 UTC (rev 141) @@ -18,6 +18,7 @@ #include "KTutorialEditor.h" +#include <QDockWidget> #include <QTreeView> #include <KAction> @@ -27,6 +28,7 @@ #include "Step.h" #include "Tutorial.h" +#include "view/ActionListWidget.h" #include "view/EditionDialog.h" #include "view/LicenseWidget.h" #include "view/StepCustomCodeWidget.h" @@ -46,6 +48,8 @@ setupTutorialToBeEdited(); + setupDocks(); + setupActions(); setupGUI(); @@ -69,9 +73,23 @@ this, SLOT(selectStep(Step*))); } +void KTutorialEditor::setupDocks() { + mTutorialActionDock = new QDockWidget(i18nc("@title", "Edit tutorial"), + this); + mTutorialActionDock->setObjectName("editTutorialDock"); + addDockWidget(Qt::RightDockWidgetArea, mTutorialActionDock); + + mStepActionDock = new QDockWidget(i18nc("@title", "Edit step"), this); + mStepActionDock->setObjectName("editStepDock"); + addDockWidget(Qt::RightDockWidgetArea, mStepActionDock); +} + void KTutorialEditor::setupActions() { KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); + ActionListWidget* actionListWidget = + new ActionListWidget(mTutorialActionDock); + KAction* action = new KAction(this); action->setText(i18nc("@action", "Set information...")); action->setStatusTip(i18nc("@info:status", "Set the name and description " @@ -79,6 +97,7 @@ actionCollection()->addAction("setTutorialInformation", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialInformation())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set license...")); @@ -86,6 +105,7 @@ "tutorial.")); actionCollection()->addAction("setTutorialLicense", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialLicense())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set setup code...")); @@ -93,6 +113,7 @@ "executed when the tutorial starts.")); actionCollection()->addAction("setTutorialSetup", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialSetup())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set tear down code...")); @@ -100,13 +121,18 @@ "executed when the tutorial finishes.")); actionCollection()->addAction("setTutorialTearDown", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setTutorialTearDown())); + actionListWidget->addAction(action); + mTutorialActionDock->setWidget(actionListWidget); + actionListWidget = new ActionListWidget(mStepActionDock); + action = new KAction(this); action->setText(i18nc("@action", "Add step...")); action->setStatusTip(i18nc("@info:status", "Add a new step to the " "tutorial.")); actionCollection()->addAction("addStep", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(addStep())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set data...")); @@ -115,6 +141,7 @@ action->setEnabled(false); actionCollection()->addAction("setStepData", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepData())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set setup code...")); @@ -123,6 +150,7 @@ action->setEnabled(false); actionCollection()->addAction("setStepSetup", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepSetup())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Set tear down code...")); @@ -132,6 +160,7 @@ action->setEnabled(false); actionCollection()->addAction("setStepTearDown", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(setStepTearDown())); + actionListWidget->addAction(action); action = new KAction(this); action->setText(i18nc("@action", "Remove step")); @@ -140,6 +169,14 @@ action->setEnabled(false); actionCollection()->addAction("removeStep", action); connect(action, SIGNAL(triggered(bool)), this, SLOT(removeStep())); + actionListWidget->addAction(action); + + mStepActionDock->setWidget(actionListWidget); + + actionCollection()->addAction("showEditTutorialDock", + mTutorialActionDock->toggleViewAction()); + actionCollection()->addAction("showEditStepDock", + mStepActionDock->toggleViewAction()); } void KTutorialEditor::showEditionDialog(EditionWidget* editionWidget) { Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-09 21:12:53 UTC (rev 140) +++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-10 17:50:56 UTC (rev 141) @@ -45,8 +45,18 @@ * The main tree view that shows the tutorial. */ QTreeView* mTreeView; - + /** + * Dock with the actions to edit a tutorial. + */ + QDockWidget* mTutorialActionDock; + + /** + * Dock with the actions to edit a step. + */ + QDockWidget* mStepActionDock; + + /** * The tutorial being edited. */ Tutorial* mTutorial; @@ -64,6 +74,11 @@ void setupTutorialToBeEdited(); /** + * Sets up the dock widgets. + */ + void setupDocks(); + + /** * Sets up all the actions used in the application. */ void setupActions(); Modified: trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc =================================================================== --- trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-09 21:12:53 UTC (rev 140) +++ trunk/ktutorial/ktutorial-editor/src/ktutorial-editorui.rc 2010-03-10 17:50:56 UTC (rev 141) @@ -19,5 +19,12 @@ <Action name="removeStep"/> </Menu> </Menu> + <Menu name="view"> + <Menu name="panels"> + <Text context="@title:menu">Panels</Text> + <Action name="showEditTutorialDock"/> + <Action name="showEditStepDock"/> + </Menu> + </Menu> </MenuBar> </gui> Added: trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.cpp 2010-03-10 17:50:56 UTC (rev 141) @@ -0,0 +1,127 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 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 <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include "ActionListWidget.h" + +#include <QAction> +#include <QActionEvent> +#include <QToolButton> +#include <QVBoxLayout> + +/** + * A button which always leave room for an icon, even if there is none, so that + * all button texts are correctly aligned. + * + * Code got from Gwenview's app/sidebar.cpp file, licensed as GPL 2 or later + * (at the time of KDE SVN revision 968729). + */ +class ForcedIconToolButton: public QToolButton { +protected: + + virtual void paintEvent(QPaintEvent* event) { + forceIcon(); + QToolButton::paintEvent(event); + } + +private: + + void forceIcon() { + if (!icon().isNull()) { + return; + } + + // Assign an empty icon to the button if there is no icon associated + // with the action so that all button texts are correctly aligned. + QSize wantedSize = iconSize(); + if (mEmptyIcon.isNull() || + mEmptyIcon.actualSize(wantedSize) != wantedSize) { + QPixmap pix(wantedSize); + pix.fill(Qt::transparent); + mEmptyIcon.addPixmap(pix); + } + setIcon(mEmptyIcon); + } + + QIcon mEmptyIcon; + +}; + +//public: + +ActionListWidget::ActionListWidget(QWidget* parent): QWidget(parent) { + QVBoxLayout* layout = new QVBoxLayout(this); + layout->addStretch(); + layout->setMargin(0); + layout->setSpacing(0); + + setLayout(layout); +} + +//protected: + +void ActionListWidget::actionEvent(QActionEvent* event) { + if (event->type() == QEvent::ActionAdded) { + addToolButtonForAction(event->action(), + toolButtonForAction(event->before())); + return; + } + + if (event->type() == QEvent::ActionRemoved) { + QToolButton* button = toolButtonForAction(event->action()); + layout()->removeWidget(button); + button->deleteLater(); + return; + } +} + +//private: + +QToolButton* ActionListWidget::toolButtonForAction(QAction* action) { + for (int i=0; i<layout()->count(); i++ ) { + QWidget* widget = layout()->itemAt(i)->widget(); + if (widget) { + QToolButton* button = static_cast<QToolButton*>(widget); + if (button->defaultAction() == action) { + return button; + } + } + } + + return 0; +} + +void ActionListWidget::addToolButtonForAction(QAction* action, + QToolButton* before) { + Q_ASSERT(action); + + QToolButton* button = new ForcedIconToolButton(); + button->setDefaultAction(action); + button->setObjectName(action->objectName() + "ToolButton"); + button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); + button->setAutoRaise(true); + button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + + int index; + if (before == 0) { + index = layout()->count() - 1; + } else { + index = layout()->indexOf(before); + } + + static_cast<QVBoxLayout*>(layout())->insertWidget(index, button); +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.h 2010-03-10 17:50:56 UTC (rev 141) @@ -0,0 +1,81 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 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 <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#ifndef ACTIONLISTWIDGET_H +#define ACTIONLISTWIDGET_H + +#include <QWidget> + +class QToolButton; + +/** + * QWidget to show the added actions as QToolButtons. + * Standard widgets show the actions added to them in a menu. This widget shows + * them in a list of QToolButtons. Adding, inserting and removing actions is + * supported. + * + * Each QToolButton is named like its action, followed by "ToolButton". + * + * A special tool button subclass, that ensures that there is always an icon + * even if it is empty, is used. When there is an icon, Oxygen and other styles + * align the text with the icon, instead of centering it like it is done when + * there is no icon and a size policy other than fixed is used. A minimum + * expanding size policiy is used for the tool buttons to fill the whole width + * of this widget, instead of having each one its own width. + */ +class ActionListWidget: public QWidget { +Q_OBJECT +public: + + /** + * Creates a new empty ActionListWidget. + * + * @param parent The parent widget. + */ + explicit ActionListWidget(QWidget* parent = 0); + +protected: + + /** + * Handles adding, changing or deleting an action in this ActionListWidget. + * + * @param event The action event. + */ + virtual void actionEvent(QActionEvent* event); + +private: + + /** + * Returns the QToolButton in this widget that shows the given action. + * + * @param action The action to get its QToolButton. + */ + QToolButton* toolButtonForAction(QAction* action); + + /** + * Adds a new QToolButton for the given action. + * + * @param action The action to add a QToolButton for it. + * @param before Add the action before this tool button, or append it if it + * is 0. + */ + void addToolButtonForAction(QAction* action, QToolButton* before); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/ActionListWidget.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-09 21:12:53 UTC (rev 140) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-10 17:50:56 UTC (rev 141) @@ -1,6 +1,7 @@ include_directories(${KDE4_INCLUDES}) set(ktutorial_editor_view_SRCS + ActionListWidget.cpp EditionDialog.cpp EditionWidget.cpp LicenseWidget.cpp Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/ActionListWidgetTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/ActionListWidgetTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/ActionListWidgetTest.cpp 2010-03-10 17:50:56 UTC (rev 141) @@ -0,0 +1,183 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 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 <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <QtTest> + +#include "ActionListWidget.h" + +#include <QAction> +#include <QLayout> +#include <QToolButton> + +#include <KIcon> + +class ActionListWidgetTest: public QObject { +Q_OBJECT + +private slots: + + void testConstructor(); + + void testAddAction(); + void testAddActionSeveralActions(); + + void testInsertAction(); + void testInsertActionSeveralActions(); + + void testRemoveAction(); + void testRemoveActionSeveralActions(); + +private: + + QToolButton* toolButton(const ActionListWidget* widget, + const QAction* action) const; + + void assertToolButton(const ActionListWidget* widget, int index, + const QAction* action) const; + +}; + +void ActionListWidgetTest::testConstructor() { + QWidget parent; + ActionListWidget* widget = new ActionListWidget(&parent); + + QCOMPARE(widget->parent(), &parent); + QVERIFY(widget->layout()->itemAt(widget->layout()->count()-1)->spacerItem()); +} + +void ActionListWidgetTest::testAddAction() { + ActionListWidget widget; + QAction* action = new QAction(this); + action->setObjectName("action"); + + widget.addAction(action); + + assertToolButton(&widget, 0, action); + QVERIFY(widget.layout()->itemAt(widget.layout()->count()-1)->spacerItem()); +} + +void ActionListWidgetTest::testAddActionSeveralActions() { + ActionListWidget widget; + QAction* action1 = new QAction(this); + action1->setObjectName("action1"); + QAction* action2 = new QAction(this); + action2->setObjectName("action2"); + QAction* action3 = new QAction(this); + action3->setObjectName("action3"); + action3->setIcon(KIcon("kde")); + + widget.addAction(action1); + widget.addAction(action2); + widget.addAction(action3); + + assertToolButton(&widget, 0, action1); + assertToolButton(&widget, 1, action2); + assertToolButton(&widget, 2, action3); + QVERIFY(widget.layout()->itemAt(widget.layout()->count()-1)->spacerItem()); +} + +void ActionListWidgetTest::testInsertAction() { + ActionListWidget widget; + QAction* action = new QAction(this); + action->setObjectName("action"); + + widget.insertAction(0, action); + + assertToolButton(&widget, 0, action); + QVERIFY(widget.layout()->itemAt(widget.layout()->count()-1)->spacerItem()); +} + +void ActionListWidgetTest::testInsertActionSeveralActions() { + ActionListWidget widget; + QAction* action1 = new QAction(this); + action1->setObjectName("action1"); + QAction* action2 = new QAction(this); + action2->setObjectName("action2"); + QAction* action3 = new QAction(this); + action3->setObjectName("action3"); + action3->setIcon(KIcon("kde")); + + widget.insertAction(0, action3); + widget.insertAction(action3, action1); + widget.insertAction(action3, action2); + + assertToolButton(&widget, 0, action1); + assertToolButton(&widget, 1, action2); + assertToolButton(&widget, 2, action3); + QVERIFY(widget.layout()->itemAt(widget.layout()->count()-1)->spacerItem()); +} + +void ActionListWidgetTest::testRemoveAction() { + ActionListWidget widget; + QAction* action = new QAction(this); + action->setObjectName("action"); + + widget.addAction(action); + widget.removeAction(action); + + QCOMPARE(widget.layout()->count(), 1); + QVERIFY(widget.layout()->itemAt(0)->spacerItem()); +} + +void ActionListWidgetTest::testRemoveActionSeveralActions() { + ActionListWidget widget; + QAction* action1 = new QAction(this); + action1->setObjectName("action1"); + QAction* action2 = new QAction(this); + action2->setObjectName("action2"); + QAction* action3 = new QAction(this); + action3->setObjectName("action3"); + action3->setIcon(KIcon("kde")); + + widget.addAction(action1); + widget.addAction(action2); + widget.addAction(action3); + + widget.removeAction(action2); + + assertToolButton(&widget, 0, action1); + assertToolButton(&widget, 1, action3); + + widget.removeAction(action1); + widget.removeAction(action3); + + QCOMPARE(widget.layout()->count(), 1); + QVERIFY(widget.layout()->itemAt(0)->spacerItem()); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +QToolButton* ActionListWidgetTest::toolButton(const ActionListWidget* widget, + const QAction* action) const { + QString buttonName = action->objectName() + "ToolButton"; + return widget->findChild<QToolButton*>(buttonName); +} + +void ActionListWidgetTest::assertToolButton(const ActionListWidget* widget, + int index, const QAction* action) const { + QToolButton* button = toolButton(widget, action); + + QVERIFY(button); + QCOMPARE(button->parent(), widget); + QCOMPARE(button->defaultAction(), action); + QCOMPARE(widget->layout()->indexOf(button), index); +} + +QTEST_MAIN(ActionListWidgetTest) + +#include "ActionListWidgetTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/ActionListWidgetTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-09 21:12:53 UTC (rev 140) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-10 17:50:56 UTC (rev 141) @@ -17,6 +17,7 @@ ENDMACRO(UNIT_TESTS) unit_tests( + ActionListWidget EditionDialog LicenseWidget StepCustomCodeWidget @@ -39,6 +40,7 @@ ENDMACRO(MEM_TESTS) mem_tests( + ActionListWidget EditionDialog LicenseWidget StepCustomCodeWidget This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |