[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: kdeplasma-addons fifteenpuzzle patch
From: Tomaz Canabrava <tumaix () gmail ! com>
Date: 2009-09-15 21:01:06
Message-ID: 7ebbb4b50909151401j43ab7e4fv2b47392a6b4dfda1 () mail ! gmail ! com
[Download RAW message or body]
Patch for fifteen puzzle:
Adds possibility to change color of the squares
Adds possibility to change the number of squares ( probably should now
rename it to N-Puzzle ;P )
drawbacks:
the numbers are not saved when plasma is closed and reopened,
lacks KConfig for that, working on that.
["fifteen.diff" (text/x-patch)]
Index: src/fifteen.cpp
===================================================================
--- src/fifteen.cpp (revision 1021788)
+++ src/fifteen.cpp (working copy)
@@ -25,17 +25,21 @@
#include <QGraphicsSceneMouseEvent>
#include <QFontMetrics>
+#include <QDebug>
#include <KDebug>
#include <KGlobalSettings>
#include "plasma/animator.h"
+#include <KMessageBox>
-Fifteen::Fifteen(QGraphicsItem *parent)
+Fifteen::Fifteen(QGraphicsItem* parent, int level)
: QGraphicsWidget(parent)
{
- m_pieces.resize(16);
+ m_level = level;
+ m_pieces.resize((m_level * m_level));
m_splitPixmap = false;
m_numerals = true;
+ m_color = QColor(Qt::green);
m_svg = new Plasma::Svg();
shuffle();
@@ -48,9 +52,34 @@
delete m_svg;
}
+void Fifteen::changeLevel(int i)
+{
+ m_level = i;
+ shuffle();
+ updatePieces();
+}
+
+void Fifteen::changeColor(const QColor& c)
+{
+ m_color = c;
+ kDebug() << "COLOR " << c;
+ shuffle();
+ updatePieces();
+}
+
+int Fifteen::getLevel()
+{
+ return m_level;
+}
+
+QColor Fifteen:: getColor()
+{
+ return m_color;
+}
+
void Fifteen::clearPieces()
{
- for (int i = 0; i < 16; ++i)
+ for (int i = 0; i < (m_level * m_level); ++i)
delete m_pieces[i];
}
@@ -59,8 +88,9 @@
qsrand(time(0));
qDeleteAll(m_pieces);
m_pieces.fill(NULL);
- int numPiecesLeft = 16;
- for (int i = 0; i < 16; ++i) {
+ m_pieces.resize((m_level * m_level));
+ int numPiecesLeft = (m_level * m_level);
+ for (int i = 0; i < (m_level * m_level); ++i) {
int randIndex = qrand() % numPiecesLeft;
int rand = 0;
--numPiecesLeft;
@@ -111,13 +141,15 @@
Q_UNUSED(event);
QSizeF size = contentsRect().size();
- int width = size.width() / 4;
- int height = size.height() / 4;
+ int width = size.width() / m_level;
+ int height = size.height() / m_level;
QString test = "99";
QFont f = font();
int smallest = KGlobalSettings::smallestReadableFont().pixelSize();
- int fontSize = 14;
+ Piece *x = m_pieces[0];
+ qDebug() << "HEIGHT " << x->boundingRect().height();
+ int fontSize = x?x->boundingRect().height()/3:14;
f.setBold(true);
f.setPixelSize(fontSize);
@@ -143,12 +175,12 @@
bool Fifteen::isSolvable()
{
- int fields[16];
+ int fields[(m_level * m_level)];
bool odd_even_solvable=0;
- for (int i = 0; i < 16; ++i) {
+ for (int i = 0; i < (m_level * m_level); ++i) {
fields[i] = m_pieces[i]->getId();
if (fields[i] == 0) {
- fields[i] = 16;
+ fields[i] = (m_level * m_level);
switch (i) {
case 0: case 2: case 5: case 7:
case 8: case 10: case 13: case 15: odd_even_solvable = 1; break;
@@ -159,7 +191,7 @@
}
bool odd_even_permutations = 1;
- for (int i = 0; i < 16; ++i) {
+ for (int i = 0; i < (m_level * m_level); ++i) {
int field = fields[i];
while (field != i + 1) {
int temp_field = fields[field - 1];
@@ -189,8 +221,8 @@
void Fifteen::updatePieces()
{
QSizeF size = contentsRect().size();
- int width = size.width() / 4;
- int height = size.height() / 4;
+ int width = size.width() / m_level;
+ int height = size.height() / m_level;
if (m_splitPixmap) {
m_svg->resize(size);
@@ -198,7 +230,8 @@
m_svg->resize(width, height);
}
- for (int i = 0; i < 16; ++i) {
+ for (int i = 0; i < (m_level * m_level); ++i) {
+ kDebug() << "OI " << i;
m_pieces[i]->showNumeral(m_numerals);
m_pieces[i]->setSplitImage(m_splitPixmap);
m_pieces[i]->setSize(QSizeF(width, height));
@@ -246,8 +279,8 @@
Piece* Fifteen::itemAt(int gameX, int gameY)
{
- int gamePos = (gameY * 4) + gameX;
- for (int i = 0; i < 16; i++) {
+ int gamePos = (gameY * m_level) + gameX;
+ for (int i = 0; i < (m_level * m_level); i++) {
if (m_pieces[i]->getGamePos() == gamePos) {
return m_pieces[i];
}
@@ -257,8 +290,8 @@
void Fifteen::swapPieceWithBlank(Piece *item)
{
- int width = contentsRect().size().width() / 4;
- int height = contentsRect().size().height() / 4;
+ int width = contentsRect().size().width() / m_level;
+ int height = contentsRect().size().height() / m_level;
// swap widget positions
QPointF pos = QPointF(item->getGameX() * width, item->getGameY() * height);
Index: src/fifteenPuzzleConfig.cpp
===================================================================
--- src/fifteenPuzzleConfig.cpp (revision 1021788)
+++ src/fifteenPuzzleConfig.cpp (working copy)
@@ -34,6 +34,9 @@
ui.setupUi(mainWidget());
connect(ui.pb_shuffle, SIGNAL(clicked()), this, SIGNAL(shuffle()));
+ connect(ui.spinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
+ connect(ui.kcolorcombo, SIGNAL(activated(QColor)), this, \
SIGNAL(colorChanged(QColor))); +
}
void FifteenPuzzleConfig::slotButtonClicked(int button)
Index: src/fifteen.h
===================================================================
--- src/fifteen.h (revision 1021788)
+++ src/fifteen.h (working copy)
@@ -30,15 +30,19 @@
{
Q_OBJECT
public:
- Fifteen(QGraphicsItem *parent = 0);
+ Fifteen(QGraphicsItem *parent = 0, int level = 4);
~Fifteen();
void updatePieces();
+ int getLevel();
+ QColor getColor();
public slots:
void piecePressed(Piece *item);
void setImage(const QString &path, bool identicalPieces);
void setShowNumerals(bool show);
void shuffle();
+ void changeLevel(int i);
+ void changeColor(const QColor& c);
protected:
void resizeEvent(QGraphicsSceneResizeEvent * event);
@@ -49,6 +53,8 @@
void swapPieceWithBlank(Piece *item);
void clearPieces();
bool isSolvable();
+ int m_level;
+ QColor m_color;
QVector<Piece *> m_pieces;
Piece *m_blank;
Index: src/fifteenPuzzleConfig.h
===================================================================
--- src/fifteenPuzzleConfig.h (revision 1021788)
+++ src/fifteenPuzzleConfig.h (working copy)
@@ -21,9 +21,10 @@
#define FIFTEENPUZZLECONFIG_H
#include <KDialog>
+#include <KColorCombo>
#include "ui_fifteenPuzzleConfig.h"
-
+class Fifteen;
class FifteenPuzzleConfig : public KDialog
{
Q_OBJECT
@@ -37,6 +38,8 @@
signals:
void shuffle();
+ void colorChanged(QColor c);
+ void valueChanged(int i);
};
#endif
Index: src/piece.cpp
===================================================================
--- src/piece.cpp (revision 1021788)
+++ src/piece.cpp (working copy)
@@ -26,16 +26,18 @@
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QFontMetrics>
-
+#include "fifteen.h"
#include <KDebug>
-Piece::Piece(int id, QGraphicsItem *parent, Plasma::Svg *svg, int gamePos)
+Piece::Piece(int id, Fifteen* parent, Plasma::Svg* svg, int gamePos)
: QGraphicsItem(parent)
{
m_id = id;
+ m_fifteen = parent;
m_numeral = true;
m_gamePos = gamePos;
m_svg = svg;
+ m_bg = new QGraphicsRectItem(this);
setCacheMode(DeviceCoordinateCache);
}
@@ -46,12 +48,12 @@
int Piece::getGameX()
{
- return m_gamePos % 4;
+ return m_gamePos % m_fifteen->getLevel();
}
int Piece::getGameY()
{
- return m_gamePos / 4;
+ return m_gamePos / m_fifteen->getLevel();
}
int Piece::getGamePos()
@@ -99,6 +101,10 @@
m_svg->paint(painter, QPointF(0, 0), "piece_" + QString::number(m_id));
} else {
// here we assume that the svg has already been resized correctly by \
Fifteen::updatePixmaps() + QColor c(m_fifteen->getColor());
+ c.setAlphaF(0.5);
+ painter->setBrush(c);
+ painter->drawRect(boundingRect());
m_svg->paint(painter, QPointF(0, 0));
}
@@ -127,6 +133,7 @@
painter->drawText((width / 2) - m.width(text) / 2,
(height / 2) + m.ascent() / 2,
text);
+
}
QRectF Piece::boundingRect() const
Index: src/fifteenPuzzle.cpp
===================================================================
--- src/fifteenPuzzle.cpp (revision 1021788)
+++ src/fifteenPuzzle.cpp (working copy)
@@ -29,7 +29,7 @@
//Plasma
#include <Plasma/Theme>
-static const char defaultImage[] = "fifteenPuzzle/greensquare";
+static const char defaultImage[] = "fifteenPuzzle/blanksquare";
FifteenPuzzle::FifteenPuzzle(QObject *parent, const QVariantList &args)
: Plasma::Applet(parent, args), m_configDialog(0)
@@ -80,6 +80,8 @@
{
m_configDialog = new FifteenPuzzleConfig();
connect(m_configDialog, SIGNAL(shuffle()), m_board, SLOT(shuffle()));
+ connect(m_configDialog, SIGNAL(valueChanged(int)), m_board, \
SLOT(changeLevel(int))); + connect(m_configDialog, SIGNAL(colorChanged(QColor)), \
m_board, SLOT(changeColor(QColor))); connect(parent, SIGNAL(applyClicked()), this, \
SLOT(configAccepted())); connect(parent, SIGNAL(okClicked()), this, \
SLOT(configAccepted())); parent->addPage(m_configDialog, i18n("General"), icon());
Index: src/piece.h
===================================================================
--- src/piece.h (revision 1021788)
+++ src/piece.h (working copy)
@@ -25,13 +25,15 @@
#include <QObject>
#include <Plasma/Svg>
-
+class Fifteen;
class Piece : public QObject, public QGraphicsItem
{
Q_OBJECT
+
+ Fifteen *m_fifteen;
public:
- Piece(int id, QGraphicsItem * parent, Plasma::Svg *svg, int gamePos);
+ Piece(int id, Fifteen * parent, Plasma::Svg *svg, int gamePos);
int getId();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget \
*widget); QRectF boundingRect() const;
@@ -52,6 +54,7 @@
QSizeF m_size;
QFont m_font;
Plasma::Svg *m_svg;
+ QGraphicsRectItem *m_bg;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
Index: src/fifteenPuzzleConfig.ui
===================================================================
--- src/fifteenPuzzleConfig.ui (revision 1021788)
+++ src/fifteenPuzzleConfig.ui (working copy)
@@ -1,49 +1,57 @@
-<ui version="4.0" >
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
<class>fifteenPuzzleConfig</class>
- <widget class="QWidget" name="fifteenPuzzleConfig" >
- <property name="geometry" >
+ <widget class="QWidget" name="fifteenPuzzleConfig">
+ <property name="geometry">
<rect>
<x>0</x>
<y>0</y>
- <width>363</width>
- <height>255</height>
+ <width>344</width>
+ <height>315</height>
</rect>
</property>
- <property name="windowTitle" >
+ <property name="windowTitle">
<string>Configure Fifteen Puzzle</string>
</property>
- <layout class="QVBoxLayout" name="verticalLayout_2" >
+ <layout class="QVBoxLayout" name="verticalLayout_2">
<item>
- <widget class="QGroupBox" name="gb_appearance" >
- <property name="title" >
+ <widget class="QGroupBox" name="gb_appearance">
+ <property name="title">
<string>Appearance</string>
</property>
- <property name="flat" >
+ <property name="flat">
<bool>true</bool>
</property>
- <layout class="QVBoxLayout" name="verticalLayout" >
+ <layout class="QVBoxLayout" name="verticalLayout">
<item>
- <widget class="QRadioButton" name="rb_identical" >
- <property name="text" >
- <string>Use plain pieces</string>
- </property>
- <property name="checked" >
- <bool>true</bool>
- </property>
- </widget>
+ <layout class="QGridLayout" name="gridLayout_2">
+ <item row="0" column="0">
+ <widget class="QRadioButton" name="rb_identical">
+ <property name="text">
+ <string>Use plain pieces</string>
+ </property>
+ <property name="checked">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="1">
+ <widget class="KColorCombo" name="kcolorcombo"/>
+ </item>
+ </layout>
</item>
<item>
- <layout class="QHBoxLayout" name="horizontalLayout" >
+ <layout class="QHBoxLayout" name="horizontalLayout">
<item>
- <widget class="QRadioButton" name="rb_split" >
- <property name="text" >
+ <widget class="QRadioButton" name="rb_split">
+ <property name="text">
<string>Use custom image:</string>
</property>
</widget>
</item>
<item>
- <widget class="KUrlRequester" name="urlRequester" >
- <property name="enabled" >
+ <widget class="KUrlRequester" name="urlRequester">
+ <property name="enabled">
<bool>false</bool>
</property>
</widget>
@@ -51,16 +59,16 @@
</layout>
</item>
<item>
- <layout class="QHBoxLayout" name="horizontalLayout_2" >
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
- <spacer name="horizontalSpacer" >
- <property name="orientation" >
+ <spacer name="horizontalSpacer">
+ <property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
- <property name="sizeType" >
+ <property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
- <property name="sizeHint" stdset="0" >
+ <property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
@@ -69,45 +77,82 @@
</spacer>
</item>
<item>
- <widget class="QCheckBox" name="cb_showNumerals" >
- <property name="enabled" >
+ <widget class="QCheckBox" name="cb_showNumerals">
+ <property name="enabled">
<bool>false</bool>
</property>
- <property name="text" >
+ <property name="text">
<string>Show numerals</string>
</property>
</widget>
</item>
</layout>
</item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_3">
+ <item>
+ <widget class="QLabel" name="label">
+ <property name="text">
+ <string>Difficulty</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QSpinBox" name="spinBox">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="maximum">
+ <number>25</number>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
</layout>
</widget>
</item>
<item>
- <widget class="QGroupBox" name="gb_actions" >
- <property name="title" >
+ <widget class="QGroupBox" name="gb_actions">
+ <property name="title">
<string>Actions</string>
</property>
- <property name="flat" >
+ <property name="flat">
<bool>true</bool>
</property>
- <layout class="QVBoxLayout" >
+ <layout class="QVBoxLayout" name="verticalLayout_4">
<item>
- <widget class="QPushButton" name="pb_shuffle" >
- <property name="text" >
- <string>Shuffle Pieces</string>
- </property>
- </widget>
+ <layout class="QVBoxLayout" name="verticalLayout_3">
+ <item>
+ <layout class="QGridLayout" name="gridLayout"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="pb_shuffle">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>Shuffle Pieces</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer>
- <property name="orientation" >
+ <property name="orientation">
<enum>Qt::Vertical</enum>
</property>
- <property name="sizeHint" stdset="0" >
+ <property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
@@ -123,6 +168,11 @@
<extends>QFrame</extends>
<header>kurlrequester.h</header>
</customwidget>
+ <customwidget>
+ <class>KColorCombo</class>
+ <extends>QComboBox</extends>
+ <header>kcolorcombo.h</header>
+ </customwidget>
</customwidgets>
<resources/>
<connections>
@@ -132,13 +182,13 @@
<receiver>cb_showNumerals</receiver>
<slot>setEnabled(bool)</slot>
<hints>
- <hint type="sourcelabel" >
+ <hint type="sourcelabel">
<x>146</x>
- <y>81</y>
+ <y>92</y>
</hint>
- <hint type="destinationlabel" >
- <x>146</x>
- <y>146</y>
+ <hint type="destinationlabel">
+ <x>184</x>
+ <y>133</y>
</hint>
</hints>
</connection>
@@ -148,15 +198,31 @@
<receiver>urlRequester</receiver>
<slot>setEnabled(bool)</slot>
<hints>
- <hint type="sourcelabel" >
+ <hint type="sourcelabel">
<x>146</x>
- <y>81</y>
+ <y>92</y>
</hint>
- <hint type="destinationlabel" >
- <x>171</x>
- <y>114</y>
+ <hint type="destinationlabel">
+ <x>329</x>
+ <y>104</y>
</hint>
</hints>
</connection>
+ <connection>
+ <sender>rb_split</sender>
+ <signal>toggled(bool)</signal>
+ <receiver>kcolorcombo</receiver>
+ <slot>setDisabled(bool)</slot>
+ <hints>
+ <hint type="sourcelabel">
+ <x>45</x>
+ <y>96</y>
+ </hint>
+ <hint type="destinationlabel">
+ <x>201</x>
+ <y>58</y>
+ </hint>
+ </hints>
+ </connection>
</connections>
</ui>
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt (revision 1021788)
+++ CMakeLists.txt (working copy)
@@ -1,5 +1,16 @@
project(plasma-fifteenPuzzle)
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
+
+find_package(KDE4 REQUIRED)
+include (KDE4Defaults)
+include(MacroLibrary)
+include(MacroOptionalDependPackage)
+
+find_package(KDE4Workspace REQUIRED)
+find_package(KdepimLibs REQUIRED)
+find_package(Boost)
+
add_subdirectory(icons)
include_directories( ${KDE4_INCLUDES} )
@@ -14,4 +25,5 @@
install(TARGETS plasma_applet_fifteenPuzzle DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES plasma-applet-fifteenPuzzle.desktop DESTINATION \
${SERVICES_INSTALL_DIR}) install(FILES images/greensquare.svgz DESTINATION \
${DATA_INSTALL_DIR}/desktoptheme/default/fifteenPuzzle/) +install(FILES \
images/blanksquare.svg DESTINATION \
${DATA_INSTALL_DIR}/desktoptheme/default/fifteenPuzzle/)
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic