- Allow switching colors with mouse wheel
authorunc0rr
Sun, 27 May 2012 23:42:43 +0400
changeset 7130 fcab1fd02bc6
parent 7128 574b385ce7df
child 7131 c0c787eb526e
child 7132 baf3351646f4
- Allow switching colors with mouse wheel - Use indices instead of color values - Introduce a model for colors - Some small fixes, .pro can be used to build again - Yay, tested it to work via network (and fixed a bug!) (TODO: pass colors to engine by index)
QTfrontend/game.cpp
QTfrontend/hwconsts.cpp.in
QTfrontend/hwconsts.h
QTfrontend/main.cpp
QTfrontend/net/newnetclient.cpp
QTfrontend/team.cpp
QTfrontend/team.h
QTfrontend/ui/page/pageplayrecord.cpp
QTfrontend/ui/widget/colorwidget.cpp
QTfrontend/ui/widget/colorwidget.h
QTfrontend/ui/widget/frameTeam.cpp
QTfrontend/ui/widget/frameTeam.h
QTfrontend/ui/widget/teamselhelper.cpp
QTfrontend/ui/widget/teamselhelper.h
project_files/hedgewars.pro
--- a/QTfrontend/game.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/game.cpp	Sun May 27 23:42:43 2012 +0400
@@ -124,7 +124,7 @@
 
     HWTeam team1;
     team1.setDifficulty(0);
-    team1.setColor(QColor(colors[0]));
+    team1.setColor(0);
     team1.setNumHedgehogs(4);
     HWNamegen::teamRandomNames(team1,true);
     HWProto::addStringListToBuffer(teamscfg,
@@ -132,7 +132,7 @@
 
     HWTeam team2;
     team2.setDifficulty(4);
-    team2.setColor(QColor(colors[1]));
+    team2.setColor(1);
     team2.setNumHedgehogs(4);
     do
         HWNamegen::teamRandomNames(team2,true);
--- a/QTfrontend/hwconsts.cpp.in	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/hwconsts.cpp.in	Sun May 27 23:42:43 2012 +0400
@@ -16,6 +16,8 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
+#include <QStandardItemModel>
+
 #include "hwconsts.h"
 
 QString * cProtoVer = new QString("${HEDGEWARS_PROTO_VER}");
@@ -70,3 +72,19 @@
 
 int season = SEASON_NONE;
 int years_since_foundation = 0;
+
+QStandardItemModel * colorsModel;
+
+void hwConstsInit()
+{
+    colorsModel = new QStandardItemModel();
+
+    int i = 0;
+    while(colors[i])
+    {
+        QStandardItem * item = new QStandardItem();
+        item->setData(QColor(colors[i]));
+        colorsModel->appendRow(item);
+        ++i;
+    }
+}
--- a/QTfrontend/hwconsts.h	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/hwconsts.h	Sun May 27 23:42:43 2012 +0400
@@ -38,13 +38,14 @@
 extern int cMaxTeams;
 extern int cMinServerVersion;
 
-class QStringListModel;
+class QStandardItemModel;
 
 extern QString * cDefaultAmmoStore;
 extern int cAmmoNumber;
 extern QList< QPair<QString, QString> > cDefaultAmmos;
 
-extern unsigned int colors[];
+//extern unsigned int colors[];
+extern QStandardItemModel * colorsModel;
 
 extern QString * netHost;
 extern quint16 netPort;
@@ -59,6 +60,8 @@
 //Could be used to implement a text/graphic like "This is the xxth birthday of hedgewars" or similar
 extern int years_since_foundation;
 
+void hwConstsInit();
+
 #endif
 
 #define HEDGEHOGS_PER_TEAM           8
--- a/QTfrontend/main.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/main.cpp	Sun May 27 23:42:43 2012 +0400
@@ -103,6 +103,9 @@
 int main(int argc, char *argv[])
 {
     HWApplication app(argc, argv);
+
+    hwConstsInit();
+
     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
 
     QStringList arguments = app.arguments();
--- a/QTfrontend/net/newnetclient.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/net/newnetclient.cpp	Sun May 27 23:42:43 2012 +0400
@@ -106,7 +106,7 @@
 {
     QString cmd = QString("ADD_TEAM") + delimeter +
                   team.name() + delimeter +
-                  team.color().name() + delimeter +
+                  QString::number(team.color()) + delimeter +
                   team.grave() + delimeter +
                   team.fort() + delimeter +
                   team.voicepack() + delimeter +
@@ -613,7 +613,7 @@
             return;
         }
         HWTeam tmptm(lst[1]);
-        tmptm.setColor(QColor(lst[2]));
+        tmptm.setColor(lst[2].toInt());
         emit teamColorChanged(tmptm);
         return;
     }
@@ -687,7 +687,7 @@
         RawSendNet(QString("TEAM_COLOR%1%2%1%3")
                    .arg(delimeter)
                    .arg(team.name())
-                   .arg(team.color().name()));
+                   .arg(team.color()));
 }
 
 void HWNewNet::onParamChanged(const QString & param, const QStringList & value)
--- a/QTfrontend/team.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/team.cpp	Sun May 27 23:42:43 2012 +0400
@@ -22,6 +22,7 @@
 #include <QLineEdit>
 #include <QCryptographicHash>
 #include <QSettings>
+#include <QStandardItemModel>
 
 #include "team.h"
 #include "hwform.h"
@@ -248,10 +249,10 @@
     QStringList sl;
     if (m_isNetTeam)
     {
-        sl.push_back(QString("eaddteam %3 %1 %2").arg(m_color.rgb() & 0xffffff).arg(m_name).arg(QString(QCryptographicHash::hash(m_owner.toLatin1(), QCryptographicHash::Md5).toHex())));
+        sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(QString(QCryptographicHash::hash(m_owner.toLatin1(), QCryptographicHash::Md5).toHex())));
         sl.push_back("erdriven");
     }
-    else sl.push_back(QString("eaddteam %3 %1 %2").arg(m_color.rgb() & 0xffffff).arg(m_name).arg(playerHash));
+    else sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash));
 
     sl.push_back(QString("egrave " + m_grave));
     sl.push_back(QString("efort " + m_fort));
@@ -334,11 +335,17 @@
 }
 
 // color
-QColor HWTeam::color() const
+int HWTeam::color() const
 {
     return m_color;
 }
-void HWTeam::setColor(const QColor & color)
+
+QColor HWTeam::qcolor() const
+{
+    return colorsModel->item(m_color)->data().value<QColor>();
+}
+
+void HWTeam::setColor(int color)
 {
     m_color = color;
 }
@@ -422,4 +429,3 @@
 {
     m_wins++;
 }
-
--- a/QTfrontend/team.h	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/team.h	Sun May 27 23:42:43 2012 +0400
@@ -63,7 +63,8 @@
 
         // attribute getters
         unsigned int campaignProgress() const;
-        QColor color() const;
+        int color() const;
+        QColor qcolor() const;
         unsigned int difficulty() const;
         QString flag() const;
         QString fort() const;
@@ -78,7 +79,6 @@
 
         // attribute setters
         void bindKey(unsigned int idx, const QString & key);
-        void setColor(const QColor & color);
         void setDifficulty(unsigned int level);
         void setFlag(const QString & flag);
         void setFort(const QString & fort);
@@ -100,6 +100,8 @@
         bool operator < (const HWTeam& t1) const;
         HWTeam & operator = (const HWTeam & other);
 
+public slots:
+        void setColor(int color);
 
     private:
 
@@ -117,7 +119,7 @@
 
         // class members that contain info for the current game setup
         quint8 m_numHedgehogs;
-        QColor m_color;
+        int m_color;
         bool m_isNetTeam;
         QString m_owner;
 
--- a/QTfrontend/ui/page/pageplayrecord.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/ui/page/pageplayrecord.cpp	Sun May 27 23:42:43 2012 +0400
@@ -110,7 +110,7 @@
 
 void PagePlayDemo::refresh()
 {
-    if (this->isVisible());
+    if (this->isVisible())
         FillFromDir(recType);
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/colorwidget.cpp	Sun May 27 23:42:43 2012 +0400
@@ -0,0 +1,71 @@
+#include <QStandardItemModel>
+#include <QMouseEvent>
+#include <QWheelEvent>
+
+#include "colorwidget.h"
+#include "hwconsts.h"
+
+ColorWidget::ColorWidget(QStandardItemModel *colorsModel, QWidget *parent) :
+    QWidget(parent)
+{
+    m_colorsModel = colorsModel;
+
+    setColor(0);
+    setStyleSheet("");
+    setAutoFillBackground(true);
+
+    connect(m_colorsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataChanged(QModelIndex,QModelIndex)));
+}
+
+ColorWidget::~ColorWidget()
+{
+
+}
+
+void ColorWidget::setColor(int color)
+{
+    Q_ASSERT_X(color >= 0 && color < m_colorsModel->rowCount(), "ColorWidget::setColor", "Color index out of range");
+
+    m_color = color;
+
+    QStandardItem * item = m_colorsModel->item(m_color);
+
+    QPalette p = palette();
+    p.setColor(QPalette::Window, item->data().value<QColor>());
+    setPalette(p);
+
+    emit colorChanged(m_color);
+}
+
+int ColorWidget::getColor()
+{
+    return m_color;
+}
+
+void ColorWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+{
+    if(m_color >= topLeft.row() && m_color <= bottomRight.row())
+        setColor(m_color);
+}
+
+void ColorWidget::mousePressEvent(QMouseEvent * event)
+{
+    switch(event->button())
+    {
+        case Qt::LeftButton:
+            setColor((m_color + 1) % m_colorsModel->rowCount());
+            break;
+        case Qt::RightButton:
+            setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
+            break;
+        default:;
+    }
+}
+
+void ColorWidget::wheelEvent(QWheelEvent *event)
+{
+    if(event->delta() > 0)
+        setColor((m_color + 1) % m_colorsModel->rowCount());
+    else
+        setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/colorwidget.h	Sun May 27 23:42:43 2012 +0400
@@ -0,0 +1,40 @@
+#ifndef COLORWIDGET_H
+#define COLORWIDGET_H
+
+#include <QWidget>
+#include <QModelIndex>
+
+namespace Ui {
+class ColorWidget;
+}
+
+class QStandardItemModel;
+
+class ColorWidget : public QWidget
+{
+    Q_OBJECT
+    
+public:
+    explicit ColorWidget(QStandardItemModel *colorsModel, QWidget *parent = 0);
+    ~ColorWidget();
+
+    void setColors(QStandardItemModel * colorsModel);
+    void setColor(int color);
+    int getColor();
+
+signals:
+    void colorChanged(int color);
+    
+private:
+    int m_color;
+    QStandardItemModel * m_colorsModel;
+
+private slots:
+    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+
+protected:
+    void mousePressEvent(QMouseEvent * event);
+    void wheelEvent(QWheelEvent * event);
+};
+
+#endif // COLORWIDGET_H
--- a/QTfrontend/ui/widget/frameTeam.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/ui/widget/frameTeam.cpp	Sun May 27 23:42:43 2012 +0400
@@ -20,6 +20,7 @@
 #include <QResizeEvent>
 #include <QCoreApplication>
 #include <QPalette>
+#include <QStandardItemModel>
 
 #include "frameTeam.h"
 #include "teamselhelper.h"
@@ -36,10 +37,6 @@
     mainLayout.setSpacing(1);
     mainLayout.setContentsMargins(4, 4, 4, 4);
 
-    int i = 0;
-    while(colors[i] != 0)
-        availableColors.push_back(QColor(colors[i++]));
-
     resetColors();
     this->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
 }
@@ -57,19 +54,13 @@
 
 void FrameTeams::resetColors()
 {
-    currentColor = availableColors.last(); // ensure next color is the first one
+    currentColor = colorsModel->rowCount() - 1; // ensure next color is the first one
 }
 
-QColor FrameTeams::getNextColor() const
+int FrameTeams::getNextColor()
 {
-    int idx = availableColors.indexOf(currentColor);
-
-    idx++;
-
-    if (idx >= availableColors.size())
-        idx = 0;
-
-    return availableColors.at(idx);
+    currentColor = (currentColor + 1) % colorsModel->rowCount();
+    return currentColor;
 }
 
 void FrameTeams::addTeam(HWTeam team, bool willPlay)
--- a/QTfrontend/ui/widget/frameTeam.h	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/ui/widget/frameTeam.h	Sun May 27 23:42:43 2012 +0400
@@ -42,7 +42,7 @@
         void setHHNum(const HWTeam& team);
         void setTeamColor(const HWTeam& team);
         void setInteractivity(bool interactive);
-        QColor getNextColor() const;
+        int getNextColor();
         QSize sizeHint() const;
 
     signals:
@@ -55,8 +55,7 @@
     private:
         const int maxHedgehogsPerGame;
         int overallHedgehogs;
-        QList<QColor> availableColors;
-        QColor currentColor;
+        int currentColor;
 
         void emitTeamColorChanged(const HWTeam& team);
 
--- a/QTfrontend/ui/widget/teamselhelper.cpp	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/ui/widget/teamselhelper.cpp	Sun May 27 23:42:43 2012 +0400
@@ -26,6 +26,7 @@
 #include "teamselhelper.h"
 #include "hwconsts.h"
 #include "frameTeam.h"
+#include "colorwidget.h"
 
 void TeamLabel::teamButtonClicked()
 {
@@ -34,7 +35,7 @@
 
 TeamShowWidget::TeamShowWidget(HWTeam team, bool isPlaying, FrameTeams * parent) :
     QWidget(parent), mainLayout(this), m_team(team), m_isPlaying(isPlaying), phhoger(0),
-    colorButt(0)
+    colorWidget(0)
 {
     m_parentFrameTeams = parent;
     QPalette newPalette = palette();
@@ -67,17 +68,15 @@
     if(m_isPlaying)
     {
         // team color
-        colorButt = new QPushButton(this);
-        colorButt->setMaximumWidth(26);
-        colorButt->setMinimumHeight(26);
-        colorButt->setGeometry(0, 0, 26, 26);
-
-        incrementTeamColor();
-        connect(colorButt, SIGNAL(clicked()), this, SLOT(incrementTeamColor()));
-
-        colorButt->setContextMenuPolicy(Qt::CustomContextMenu);
-        connect(colorButt, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(decrementTeamColor()));
-        mainLayout.addWidget(colorButt);
+        colorWidget = new ColorWidget(colorsModel, this);
+        colorWidget->setMinimumWidth(26);
+        colorWidget->setMaximumWidth(26);
+        colorWidget->setMinimumHeight(26);
+        colorWidget->setMaximumHeight(26);
+        //colorWidget->setGeometry(0, 0, 26, 26);
+        connect(colorWidget, SIGNAL(colorChanged(int)), this, SLOT(onColorChanged(int)));
+        colorWidget->setColor(m_parentFrameTeams->getNextColor());
+        mainLayout.addWidget(colorWidget);
 
         phhoger = new CHedgehogerWidget(QImage(":/res/hh25x25.png"), QImage(":/res/hh25x25grey.png"), this);
         connect(phhoger, SIGNAL(hedgehogsNumChanged()), this, SLOT(hhNumChanged()));
@@ -99,7 +98,7 @@
         butt->setEnabled(interactive);
     }
 
-    colorButt->setEnabled(interactive);
+    colorWidget->setEnabled(interactive);
     phhoger->setEnabled(interactive);
 }
 
@@ -128,35 +127,16 @@
   return params;
 }*/
 
-void TeamShowWidget::incrementTeamColor()
-{
-    changeTeamColor(m_parentFrameTeams->getNextColor());
-}
-void TeamShowWidget::decrementTeamColor()
+
+void TeamShowWidget::changeTeamColor(int color)
 {
-    const QList<QColor> & availColors = m_parentFrameTeams->availableColors;
-    int idx = availColors.indexOf(m_parentFrameTeams->currentColor);
-
-    idx--;
-
-    if (idx < 0)
-        idx = availColors.size() - 1;
-
-    changeTeamColor(availColors.at(idx));
+    colorWidget->setColor(color);
 }
 
-void TeamShowWidget::changeTeamColor(QColor color)
+void TeamShowWidget::onColorChanged(int color)
 {
-    QColor & curColor = m_parentFrameTeams->currentColor;
-    curColor = color;
+    m_team.setColor(color);
 
-    colorButt->setStyleSheet(QString("QPushButton{"
-                                     "background-color: %1;"
-                                     "border-width: 1px;"
-                                     "border-radius: 2px;"
-                                     "}").arg(curColor.name()));
-
-    m_team.setColor(color);
     emit teamColorChanged(m_team);
 }
 
--- a/QTfrontend/ui/widget/teamselhelper.h	Sat May 26 16:50:51 2012 -0400
+++ b/QTfrontend/ui/widget/teamselhelper.h	Sun May 27 23:42:43 2012 +0400
@@ -28,6 +28,8 @@
 #include "teamselect.h"
 #include "hedgehogerWidget.h"
 
+class ColorWidget;
+
 class TeamLabel : public QLabel
 {
         Q_OBJECT
@@ -48,13 +50,12 @@
         Q_OBJECT
 
     public slots:
-        void incrementTeamColor();
-        void decrementTeamColor();
-        void changeTeamColor(QColor color=QColor());
+        void changeTeamColor(int color = 0);
         void hhNumChanged();
 
     private slots:
         void activateTeam();
+        void onColorChanged(int color);
 
     public:
         TeamShowWidget(HWTeam team, bool isPlaying, FrameTeams * parent);
@@ -69,10 +70,9 @@
         HWTeam m_team;
         bool m_isPlaying;
         CHedgehogerWidget* phhoger;
-        QPushButton* colorButt;
+        ColorWidget* colorWidget;
         QPushButton* butt;
         FrameTeams * m_parentFrameTeams;
-// QPushButton* bText;
 
     signals:
         void teamStatusChanged(HWTeam team);
--- a/project_files/hedgewars.pro	Sat May 26 16:50:51 2012 -0400
+++ b/project_files/hedgewars.pro	Sun May 27 23:42:43 2012 +0400
@@ -26,7 +26,6 @@
     ../QTfrontend/model/MapModel.h \
     ../QTfrontend/model/ammoSchemeModel.h \
     ../QTfrontend/model/netserverslist.h \
-    ../QTfrontend/model/hats.h \
     ../QTfrontend/ui/page/pagedrawmap.h \
     ../QTfrontend/ui/page/pagedata.h \
     ../QTfrontend/ui/page/pagetraining.h \
@@ -102,12 +101,14 @@
     ../QTfrontend/ui/widget/qpushbuttonwithsound.h \
     ../QTfrontend/ui/page/pagefeedback.h \
     ../QTfrontend/model/roomslistmodel.h \
-    ../QTfrontend/ui/dialog/input_password.h
+    ../QTfrontend/ui/dialog/input_password.h \
+    ../QTfrontend/ui/widget/colorwidget.h \
+    ../QTfrontend/model/HatModel.h \
+    ../QTfrontend/model/GameStyleModel.h
 
 SOURCES += ../QTfrontend/model/ammoSchemeModel.cpp \
     ../QTfrontend/model/MapModel.cpp \
     ../QTfrontend/model/ThemeModel.cpp \
-    ../QTfrontend/model/hats.cpp \
     ../QTfrontend/model/netserverslist.cpp \
     ../QTfrontend/ui/qaspectratiolayout.cpp \
     ../QTfrontend/ui/page/pagemain.cpp \
@@ -182,7 +183,10 @@
     ../QTfrontend/ui/widget/qpushbuttonwithsound.cpp \
     ../QTfrontend/ui/page/pagefeedback.cpp \
     ../QTfrontend/model/roomslistmodel.cpp \
-    ../QTfrontend/ui/dialog/input_password.cpp
+    ../QTfrontend/ui/dialog/input_password.cpp \
+    ../QTfrontend/ui/widget/colorwidget.cpp \
+    ../QTfrontend/model/HatModel.cpp \
+    ../QTfrontend/model/GameStyleModel.cpp
 
 win32 {
     SOURCES += ../QTfrontend/xfire.cpp
@@ -236,3 +240,5 @@
     CONFIG += warn_on x86
     #CONFIG += x86 ppc x86_64 ppc64
 }
+
+FORMS +=