[Ktutorial-commits] SF.net SVN: ktutorial:[228] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-04-13 18:06:34
|
Revision: 228 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=228&view=rev Author: danxuliu Date: 2010-04-13 18:06:28 +0000 (Tue, 13 Apr 2010) Log Message: ----------- Add utility classes to highlight widgets with an animation. Modified Paths: -------------- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-library/src/extendedinformation/ trunk/ktutorial/ktutorial-library/src/extendedinformation/CMakeLists.txt trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.cpp trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.h trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.cpp trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.h trunk/ktutorial/ktutorial-library/tests/extendedinformation/ trunk/ktutorial/ktutorial-library/tests/extendedinformation/CMakeLists.txt trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterManagerTest.cpp trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterTest.cpp Modified: trunk/ktutorial/ktutorial-library/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2010-04-01 03:46:38 UTC (rev 227) +++ trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2010-04-13 18:06:28 UTC (rev 228) @@ -2,6 +2,7 @@ # In order to work, they must be compiled using -fPIC add_definitions("-fPIC") +add_subdirectory(extendedinformation) add_subdirectory(scripting) add_subdirectory(tutorials) add_subdirectory(view) Added: trunk/ktutorial/ktutorial-library/src/extendedinformation/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/extendedinformation/CMakeLists.txt (rev 0) +++ trunk/ktutorial/ktutorial-library/src/extendedinformation/CMakeLists.txt 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,10 @@ +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES}) + +set(ktutorial_extendedinformation_SRCS + WidgetHighlighter.cpp + WidgetHighlighterManager.cpp +) + +kde4_add_library(ktutorial_extendedinformation ${ktutorial_extendedinformation_SRCS}) + +target_link_libraries(ktutorial_extendedinformation ${KDE4_KDEUI_LIBS}) Property changes on: trunk/ktutorial/ktutorial-library/src/extendedinformation/CMakeLists.txt ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.cpp 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,125 @@ +/*************************************************************************** + * 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 <QWidget> + +#include <KColorUtils> + +#include "WidgetHighlighter.h" + +namespace extendedinformation { + +//public: + +WidgetHighlighter::WidgetHighlighter(QWidget* targetWidget): + QObject(targetWidget), + mTargetWidget(targetWidget) { + Q_ASSERT(targetWidget); + + mOriginalPalette = targetWidget->palette(); + + //TODO Use QPropertyAnimation instead? Increase Qt version requirement in + //CMakeLists.txt to Qt 4.6 if done. + mProgress = 0; + mIncreasing = true; + mStopping = false; + + int interval = 25; + int duration = 500; + mProgressForEachTick = interval / (qreal)duration; + + mTimer.setInterval(interval); + connect(&mTimer, SIGNAL(timeout()), this, SLOT(update())); +} + +WidgetHighlighter::~WidgetHighlighter() { + mTargetWidget->setPalette(mOriginalPalette); +} + +//public slots: + +void WidgetHighlighter::start() { + mStopping = false; + mIncreasing = true; + + mTimer.start(); +} + +void WidgetHighlighter::stop() { + mStopping = true; + mIncreasing = false; + + if (mProgress == 0) { + mTimer.stop(); + emit stopped(this); + } +} + +//private: + +void WidgetHighlighter::updateColorGroup(QPalette& palette, + QPalette::ColorGroup colorGroup) { + updateColorRole(palette, colorGroup, QPalette::Window, QPalette::Highlight); + updateColorRole(palette, colorGroup, QPalette::WindowText, + QPalette::HighlightedText); + updateColorRole(palette, colorGroup, QPalette::Base, QPalette::Highlight); + updateColorRole(palette, colorGroup, QPalette::Text, + QPalette::HighlightedText); + updateColorRole(palette, colorGroup, QPalette::Button, QPalette::Highlight); + updateColorRole(palette, colorGroup, QPalette::ButtonText, + QPalette::HighlightedText); +} + +void WidgetHighlighter::updateColorRole(QPalette& palette, + QPalette::ColorGroup colorGroup, + QPalette::ColorRole base, + QPalette::ColorRole tint) { + qreal amount = 0.6 * mProgress; + QColor color = KColorUtils::tint(palette.color(colorGroup, base), + palette.color(colorGroup, tint), + amount); + palette.setColor(colorGroup, base, color); +} + +//private slots: + +void WidgetHighlighter::update(){ + if (mIncreasing) { + mProgress += mProgressForEachTick; + mProgress = qMin<qreal>(1, mProgress); + mIncreasing = mProgress < 1; + } else { + mProgress -= mProgressForEachTick; + mProgress = qMax<qreal>(0, mProgress); + mIncreasing = mProgress == 0; + } + + QPalette updatedPalette = mOriginalPalette; + updateColorGroup(updatedPalette, QPalette::Active); + updateColorGroup(updatedPalette, QPalette::Inactive); + updateColorGroup(updatedPalette, QPalette::Disabled); + + mTargetWidget->setPalette(updatedPalette); + + if (mStopping && mProgress == 0) { + mTimer.stop(); + emit stopped(this); + } +} + +} Property changes on: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.h 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,162 @@ +/*************************************************************************** + * 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 WIDGETHIGHLIGHTER_H +#define WIDGETHIGHLIGHTER_H + +#include <QObject> +#include <QPalette> +#include <QTimer> + +namespace extendedinformation { + +/** + * Utility class to highlight a widget. + * WidgetHighlighter executes an animation that modifies the color palette of + * some widget, changing its colors from normal to highlighted and back again. + * + * Once started, the animation goes on indefinitely until the stop slot is + * called. The colors of the palette are set back to the original colors + * animating the change from the current color of the widget to the normal one. + * If the highlighting is started again while the stop animation is running, the + * highlighting animation is started again from the current color (that is, the + * stop animation is cancelled and a new highlight animation is started from + * that point). + * + * WidgetHighlighter should not be created directly. Instead, + * WidgetHighlighterManager should be used, as it takes care of deleting the + * highlighter when no longer needed. + * + * @see WidgetHighlighterManager + */ +class WidgetHighlighter: public QObject { +Q_OBJECT +public: + + /** + * Creates a new WidgetHighlighter for the given widget. + * + * @param targetWidget The widget to highlight. + */ + explicit WidgetHighlighter(QWidget* targetWidget); + + /** + * Destroys this WidgetHighlighter. + * It restores the original palette to the target widget. + */ + virtual ~WidgetHighlighter(); + +public Q_SLOTS: + + /** + * Starts highlighting the target widget. + * If there is a normal animation already running nothing is done. However, + * if there is a stop animation running, it is cancelled and the + * highlighting animation is started again from the current color. + */ + void start(); + + /** + * Stops highlighting the target widget. + * The animation is not stopped sharply, but a stop animation (the widget + * recovering its normal color) is executed. + */ + void stop(); + +Q_SIGNALS: + + /** + * Emitted when the animation has stopped. + * Note that it is emitted when the animation has truly stopped, that is, + * when there is not even an stop animation. + * + * @param widgetHighlighter This WidgetHighlighter. + */ + void stopped(extendedinformation::WidgetHighlighter* widgetHighlighter); + +private: + + /** + * The widget to highlight. + */ + QWidget* mTargetWidget; + + /** + * The original palette used by the widget. + */ + QPalette mOriginalPalette; + + /** + * Timer to update the colors. + */ + QTimer mTimer; + + /** + * The current progress from normal to highlighted colors. + * Range [0-1]. + */ + qreal mProgress; + + /** + * How much advances the progress when an update is done. + */ + qreal mProgressForEachTick; + + /** + * True if the widget is being highlighted, false it is being dehighlighted. + */ + bool mIncreasing; + + /** + * True if the stop animation is being run, false otherwise. + */ + bool mStopping; + + /** + * Updates the color group of the given palette based on the current + * progress. + * + * @param palette The palette to update its colors. + * @param colorGroup The color group of the palette to update. + */ + void updateColorGroup(QPalette& palette, QPalette::ColorGroup colorGroup); + + /** + * Updates the color role "base" tinting it with the color role "tint". + * How much the base color role is tinted depends on the current progress. + * + * @param palette The palette to update its colors. + * @param colorGroup The color group of the palette to update. + * @param base The color role to update. + * @param tint The color role to tint the base color role with. + */ + void updateColorRole(QPalette& palette, QPalette::ColorGroup colorGroup, + QPalette::ColorRole from, QPalette::ColorRole to); + +private Q_SLOTS: + + /** + * Updates the palette of the target widget based on the animation progress. + */ + void update(); + +}; + +} + +#endif Property changes on: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighter.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.cpp 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,68 @@ +/*************************************************************************** + * 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 <QWidget> + +#include "WidgetHighlighterManager.h" +#include "WidgetHighlighter.h" + +namespace extendedinformation { + +//public: + +WidgetHighlighterManager* WidgetHighlighterManager::self() { + return sSelf; +} + +void WidgetHighlighterManager::highlight(QWidget* widget) { + WidgetHighlighter* highlighter = widget->findChild<WidgetHighlighter*>(); + if (!highlighter) { + highlighter = new WidgetHighlighter(widget); + connect(highlighter, SIGNAL(stopped(extendedinformation::WidgetHighlighter*)), + this, SLOT(remove(extendedinformation::WidgetHighlighter*))); + } + + highlighter->start(); +} + +void WidgetHighlighterManager::stopHighlighting(QWidget* widget) { + WidgetHighlighter* highlighter = widget->findChild<WidgetHighlighter*>(); + if (!highlighter) { + return; + } + + highlighter->stop(); +} + +//private: + +WidgetHighlighterManager* WidgetHighlighterManager::sSelf = + new WidgetHighlighterManager(); + +WidgetHighlighterManager::WidgetHighlighterManager(): QObject() { +} + +//private slots: + +void WidgetHighlighterManager::remove( + extendedinformation::WidgetHighlighter* highlighter) { + highlighter->setParent(0); + delete highlighter; +} + +} Property changes on: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.h 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,96 @@ +/*************************************************************************** + * 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 WIDGETHIGHLIGHTERMANAGER_H +#define WIDGETHIGHLIGHTERMANAGER_H + +#include <QHash> +#include <QObject> + +namespace extendedinformation { +class WidgetHighlighter; +} + +namespace extendedinformation { + +/** + * Utility class to manage WidgetHighlighters. + * Starting and stopping the highlighting from WidgetHighlighterManager instead + * of using directly WidgetHighlighter ensures that no overlapping animations + * will be executed. + * + * WidgetHighlighterManager also takes care of deleting the WidgetHighlighter + * when no longer needed, that is, when they are fully stopped (once the stop + * animation has ended). + * + * @see WidgetHighlighter + */ +class WidgetHighlighterManager: public QObject { +Q_OBJECT +public: + + /** + * Returns the only instance of this class. + * + * @return The only instance of this class. + */ + static WidgetHighlighterManager* self(); + + /** + * Starts a WidgetHighlighter for the given widget. + * If the widget was already being highlighted nothing is done. + * + * @param widget The widget to highlight. + */ + void highlight(QWidget* widget); + + /** + * Stops the WidgetHighlighter of the given widget. + * + * @param widget The widget to stop highlighting. + */ + void stopHighlighting(QWidget* widget); + +private: + + /** + * The instance of this class. + */ + static WidgetHighlighterManager* sSelf; + + /** + * Creates a new WidgetHighlighterManager. + * Private to avoid classes other than self to create instances. + */ + WidgetHighlighterManager(); + +private Q_SLOTS: + + /** + * Removes the highlighter from its widget and destroys it. + * Called when the highlighter stopped. + * + * @param highlighter The highlighter to remove. + */ + void remove(extendedinformation::WidgetHighlighter* highlighter); + +}; + +} + +#endif Property changes on: trunk/ktutorial/ktutorial-library/src/extendedinformation/WidgetHighlighterManager.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2010-04-01 03:46:38 UTC (rev 227) +++ trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2010-04-13 18:06:28 UTC (rev 228) @@ -1,3 +1,4 @@ +add_subdirectory(extendedinformation) add_subdirectory(scripting) add_subdirectory(view) Added: trunk/ktutorial/ktutorial-library/tests/extendedinformation/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/tests/extendedinformation/CMakeLists.txt (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/extendedinformation/CMakeLists.txt 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,33 @@ +# Used by kde4_add_unit_test to set the full path to test executables +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${ktutorial-library_SOURCE_DIR}/src/extendedinformation ${KDE4_INCLUDES}) + +# Since Qt 4.6.0, this definition is needed for GUI testing. +# It is backwards compatible with previous Qt versions, unlike the alternative +# which is to add #include <QTestGui> in the test files. +add_definitions(-DQT_GUI_LIB) + +MACRO(UNIT_TESTS) + FOREACH(_className ${ARGN}) + set(_testName ${_className}Test) + kde4_add_unit_test(${_testName} TESTNAME ktutorial-${_testName} ${_testName}.cpp) + target_link_libraries(${_testName} ktutorial_extendedinformation ${QT_QTTEST_LIBRARY}) + ENDFOREACH(_className) +ENDMACRO(UNIT_TESTS) + +unit_tests( + WidgetHighlighter + WidgetHighlighterManager +) + +MACRO(MEM_TESTS) + FOREACH(_testname ${ARGN}) + add_test(ktutorial-mem-${_testname} ${CMAKE_CURRENT_SOURCE_DIR}/../runMemcheck.py ${CMAKE_CURRENT_BINARY_DIR}/${_testname}Test ${CMAKE_CURRENT_BINARY_DIR}) + ENDFOREACH(_testname) +ENDMACRO(MEM_TESTS) + +mem_tests( + WidgetHighlighter + WidgetHighlighterManager +) Property changes on: trunk/ktutorial/ktutorial-library/tests/extendedinformation/CMakeLists.txt ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterManagerTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterManagerTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterManagerTest.cpp 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,144 @@ +/*************************************************************************** + * 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 <QWidget> + +#include "WidgetHighlighterManager.h" +#include "WidgetHighlighter.h" + +namespace extendedinformation { + +class WidgetHighlighterManagerTest: public QObject { +Q_OBJECT +private slots: + + void testSelf(); + + void testHighlight(); + void testHighlightWidgetAlreadyHighlighted(); + void testHighlightAfterStopHighlighting(); + + void testStopHighlighting(); + + void testDeleteWidgetWhileHighlighting(); + +}; + +void WidgetHighlighterManagerTest::testSelf() { + WidgetHighlighterManager* manager1 = WidgetHighlighterManager::self(); + WidgetHighlighterManager* manager2 = WidgetHighlighterManager::self(); + + QVERIFY(manager1); + QVERIFY(manager2); + QVERIFY(manager1 == manager2); +} + +void WidgetHighlighterManagerTest::testHighlight() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighterManager* manager = WidgetHighlighterManager::self(); + + manager->highlight(&widget); + + //Give it some time to update + QTest::qWait(100); + + QCOMPARE(widget.findChildren<WidgetHighlighter*>().count(), 1); + QVERIFY(widget.findChild<WidgetHighlighter*>()); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterManagerTest::testHighlightWidgetAlreadyHighlighted() { + QWidget widget; + WidgetHighlighterManager* manager = WidgetHighlighterManager::self(); + + manager->highlight(&widget); + + WidgetHighlighter* highlighter = widget.findChild<WidgetHighlighter*>(); + + manager->highlight(&widget); + + QCOMPARE(widget.findChildren<WidgetHighlighter*>().count(), 1); + QCOMPARE(widget.findChild<WidgetHighlighter*>(), highlighter); +} + +void WidgetHighlighterManagerTest::testHighlightAfterStopHighlighting() { + QWidget widget; + WidgetHighlighterManager* manager = WidgetHighlighterManager::self(); + + manager->highlight(&widget); + + QPointer<WidgetHighlighter> highlighter = + widget.findChild<WidgetHighlighter*>(); + QVERIFY(highlighter); + + manager->stopHighlighting(&widget); + + QVERIFY(!highlighter); + + QPalette palette = widget.palette(); + manager->highlight(&widget); + + //Give it some time to update + QTest::qWait(100); + + QCOMPARE(widget.findChildren<WidgetHighlighter*>().count(), 1); + QVERIFY(widget.findChild<WidgetHighlighter*>()); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterManagerTest::testStopHighlighting() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighterManager* manager = WidgetHighlighterManager::self(); + + manager->highlight(&widget); + + //Give it some time to update + QTest::qWait(100); + + manager->stopHighlighting(&widget); + + //Give it some time to update + QTest::qWait(200); + + QCOMPARE(widget.findChildren<WidgetHighlighter*>().count(), 0); + QVERIFY(widget.palette() == palette); +} + +void WidgetHighlighterManagerTest::testDeleteWidgetWhileHighlighting() { + QWidget* widget = new QWidget(); + WidgetHighlighterManager* manager = WidgetHighlighterManager::self(); + + manager->highlight(widget); + + //Give it some time to update + QTest::qWait(100); + + delete widget; + + //No explicit check is made, if it does not crash everything is fine ;) +} + +} + +QTEST_MAIN(extendedinformation::WidgetHighlighterManagerTest) + +#include "WidgetHighlighterManagerTest.moc" Property changes on: trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterManagerTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterTest.cpp 2010-04-13 18:06:28 UTC (rev 228) @@ -0,0 +1,293 @@ +/*************************************************************************** + * 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 <QApplication> +#include <QWidget> + +#define protected public +#define private public +#include "WidgetHighlighter.h" +#undef private +#undef protected + +//extendedinformation::WidgetHighlighter* must be declared as a metatype to be +//used in qvariant_cast +Q_DECLARE_METATYPE(extendedinformation::WidgetHighlighter*); + +namespace extendedinformation { + +class WidgetHighlighterTest: public QObject { +Q_OBJECT +private slots: + + void testConstructor(); + + void testDestructor(); + + void testStart(); + void testStartAlreadyStarted(); + void testStartWhileStopAnimationIsRunning(); + + void testUpdate(); + void testUpdateWhenProgressIsAlmostOne(); + void testUpdateWhenProgressIsAlmostZero(); + + void testStop(); + void testStopAfterStopAnimationEnded(); + void testStopImmediatelyAfterStart(); + +}; + +void WidgetHighlighterTest::testConstructor() { + QWidget widget; + widget.setPalette(Qt::blue); + QApplication::setPalette(Qt::green); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + QCOMPARE(highlighter->parent(), &widget); + QVERIFY(highlighter->mOriginalPalette != QApplication::palette()); + QVERIFY(highlighter->mOriginalPalette == widget.palette()); +} + +void WidgetHighlighterTest::testDestructor() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + widget.setPalette(QPalette(Qt::green)); + delete highlighter; + + QVERIFY(widget.palette() == palette); +} + +void WidgetHighlighterTest::testStart() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testStartAlreadyStarted() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + qreal previousProgress = highlighter->mProgress; + highlighter->start(); + + //Ensure that progress is not reseted + QCOMPARE(highlighter->mProgress, previousProgress); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testStartWhileStopAnimationIsRunning() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->mProgress = 0.5; + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + highlighter->stop(); + + //Give it some time to update + QTest::qWait(100); + + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + QVERIFY(highlighter->mProgress > 0.5); + QVERIFY(highlighter->mIncreasing); + QVERIFY(!highlighter->mStopping); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testUpdate() { + QWidget widget; + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + highlighter->mTimer.stop(); + + QPalette palette = widget.palette(); + int previousProgress = highlighter->mProgress; + + highlighter->update(); + + QCOMPARE(highlighter->mProgress, + previousProgress + highlighter->mProgressForEachTick); + QVERIFY(widget.palette().color(QPalette::Window) != + palette.color(QPalette::Window)); + QVERIFY(widget.palette().color(QPalette::WindowText) != + palette.color(QPalette::WindowText)); + QVERIFY(widget.palette().color(QPalette::Base) != + palette.color(QPalette::Base)); + QVERIFY(widget.palette().color(QPalette::Text) != + palette.color(QPalette::Text)); + QVERIFY(widget.palette().color(QPalette::Button) != + palette.color(QPalette::Button)); + QVERIFY(widget.palette().color(QPalette::ButtonText) != + palette.color(QPalette::ButtonText)); +} + +void WidgetHighlighterTest::testUpdateWhenProgressIsAlmostOne() { + QWidget widget; + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + highlighter->mTimer.stop(); + + highlighter->mProgress = 0.995; + highlighter->mIncreasing = true; + highlighter->update(); + + //Don't check palette changes here, as with such a small update it could + //have been left unchanged + QCOMPARE(highlighter->mProgress, 1.0); + QCOMPARE(highlighter->mIncreasing, false); + + QPalette palette = widget.palette(); + highlighter->update(); + + QCOMPARE(highlighter->mProgress, 1 - highlighter->mProgressForEachTick); + QCOMPARE(highlighter->mIncreasing, false); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testUpdateWhenProgressIsAlmostZero() { + QWidget widget; + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + highlighter->mTimer.stop(); + + highlighter->mProgress = 0.005; + highlighter->mIncreasing = false; + highlighter->update(); + + //Don't check palette changes here, as with such a small update it could + //have been left unchanged + QCOMPARE(highlighter->mProgress, 0.0); + QCOMPARE(highlighter->mIncreasing, true); + + QPalette palette = widget.palette(); + highlighter->update(); + + QCOMPARE(highlighter->mProgress, highlighter->mProgressForEachTick); + QCOMPARE(highlighter->mIncreasing, true); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testStop() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->mProgress = 0.5; + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + highlighter->stop(); + + //Give it some time to update + QTest::qWait(200); + + QVERIFY(highlighter->mTimer.isActive()); + QVERIFY(highlighter->mProgress < 0.5); + QVERIFY(!highlighter->mIncreasing); + QVERIFY(highlighter->mStopping); + QVERIFY(widget.palette() != palette); +} + +void WidgetHighlighterTest::testStopAfterStopAnimationEnded() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->start(); + + //Give it some time to update + QTest::qWait(100); + + //extendedinformation::WidgetHighlighter* must be registered in order to be + //used with QSignalSpy + int widgetHighlighterStarType = + qRegisterMetaType<extendedinformation::WidgetHighlighter*>( + "extendedinformation::WidgetHighlighter*"); + QSignalSpy stoppedSpy(highlighter, + SIGNAL(stopped(extendedinformation::WidgetHighlighter*))); + + highlighter->stop(); + + //Give it some time to update + QTest::qWait(200); + + QVERIFY(!highlighter->mTimer.isActive()); + QVERIFY(widget.palette() == palette); + QCOMPARE(stoppedSpy.count(), 1); + QVariant argument = stoppedSpy.at(0).at(0); + QCOMPARE(argument.userType(), widgetHighlighterStarType); + QCOMPARE(qvariant_cast<extendedinformation::WidgetHighlighter*>(argument), + highlighter); +} + +void WidgetHighlighterTest::testStopImmediatelyAfterStart() { + QWidget widget; + QPalette palette = widget.palette(); + WidgetHighlighter* highlighter = new WidgetHighlighter(&widget); + + highlighter->start(); + + //extendedinformation::WidgetHighlighter* must be registered in order to be + //used with QSignalSpy + int widgetHighlighterStarType = + qRegisterMetaType<extendedinformation::WidgetHighlighter*>( + "extendedinformation::WidgetHighlighter*"); + QSignalSpy stoppedSpy(highlighter, + SIGNAL(stopped(extendedinformation::WidgetHighlighter*))); + + highlighter->stop(); + + QVERIFY(!highlighter->mTimer.isActive()); + QVERIFY(widget.palette() == palette); + QCOMPARE(stoppedSpy.count(), 1); + QVariant argument = stoppedSpy.at(0).at(0); + QCOMPARE(argument.userType(), widgetHighlighterStarType); + QCOMPARE(qvariant_cast<extendedinformation::WidgetHighlighter*>(argument), + highlighter); +} + +} + +QTEST_MAIN(extendedinformation::WidgetHighlighterTest) + +#include "WidgetHighlighterTest.moc" Property changes on: trunk/ktutorial/ktutorial-library/tests/extendedinformation/WidgetHighlighterTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |