abstract class for items container (hedgehogs num, bullets, etc.)
authordisplacer
Mon, 24 Sep 2007 20:34:28 +0000
changeset 612 333d095319de
parent 611 8cf6d27cec86
child 613 e8cf72d0e0f7
abstract class for items container (hedgehogs num, bullets, etc.)
QTfrontend/CMakeLists.txt
QTfrontend/hedgehogerWidget.cpp
QTfrontend/hedgehogerWidget.h
QTfrontend/hedgewars.pro
QTfrontend/hwform.cpp
QTfrontend/itemNum.cpp
QTfrontend/itemNum.h
QTfrontend/pages.cpp
QTfrontend/pages.h
QTfrontend/selectWeapon.cpp
QTfrontend/selectWeapon.h
QTfrontend/teamselhelper.cpp
--- a/QTfrontend/CMakeLists.txt	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/CMakeLists.txt	Mon Sep 24 20:34:28 2007 +0000
@@ -67,7 +67,8 @@
 	SDLs.cpp
 	playrecordpage.cpp
 	hwconsts.cpp
-	selectWeapon.cpp)
+	selectWeapon.cpp
+        itemNum.cpp)
 
 if (WIN32)
 	set(hwfr_src ${hwfr_src} res/hedgewars.rc)
@@ -99,7 +100,8 @@
 	chatwidget.h
 	SDLs.h
 	playrecordpage.h
-	selectWeapon.h)
+	selectWeapon.h
+        itemNum.h)
 
 set(hwfr_hdrs
 	binds.h
--- a/QTfrontend/hedgehogerWidget.cpp	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/hedgehogerWidget.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -18,26 +18,41 @@
 
 #include "hedgehogerWidget.h"
 
-#include <QMouseEvent>
-#include <QPainter>
-
 #include "frameTeam.h"
 
-CHedgehogerWidget::CHedgehogerWidget(QWidget * parent) :
-  QWidget(parent), nonInteractive(false)
+CHedgehogerWidget::CHedgehogerWidget(const QImage& im, QWidget * parent) :
+    ItemNum(im, parent)
 {
   if(parent) {
     pOurFrameTeams=dynamic_cast<FrameTeams*>(parent->parentWidget());
   }
   if(pOurFrameTeams->overallHedgehogs+4>pOurFrameTeams->maxHedgehogsPerGame) {
-    numHedgehogs=pOurFrameTeams->maxHedgehogsPerGame-pOurFrameTeams->overallHedgehogs;
-  } else numHedgehogs=4;
-  pOurFrameTeams->overallHedgehogs+=numHedgehogs;
+    numItems=pOurFrameTeams->maxHedgehogsPerGame-pOurFrameTeams->overallHedgehogs;
+  } else numItems=4;
+  pOurFrameTeams->overallHedgehogs+=numItems;
+}
+
+void CHedgehogerWidget::incItems() 
+{
+    if(numItems < 8 && pOurFrameTeams->overallHedgehogs<18) {
+      numItems++;
+      pOurFrameTeams->overallHedgehogs++;
+      emit hedgehogsNumChanged();
+    }
+}
+
+void CHedgehogerWidget::decItems()
+{
+    if(numItems > 1) {
+      numItems--;
+      pOurFrameTeams->overallHedgehogs--;
+      emit hedgehogsNumChanged();
+    }
 }
 
 CHedgehogerWidget::~CHedgehogerWidget()
 {
-  pOurFrameTeams->overallHedgehogs-=numHedgehogs;
+  pOurFrameTeams->overallHedgehogs-=numItems;
 }
 
 void CHedgehogerWidget::setNonInteractive()
@@ -45,51 +60,15 @@
   nonInteractive=true;
 }
 
-void CHedgehogerWidget::mousePressEvent ( QMouseEvent * event )
-{
-  if(nonInteractive) return;
-  if(event->button()==Qt::LeftButton) {
-    event->accept();
-    if(numHedgehogs < 8 && pOurFrameTeams->overallHedgehogs<18) {
-      numHedgehogs++;
-      pOurFrameTeams->overallHedgehogs++;
-      emit hedgehogsNumChanged();
-    }
-  } else if (event->button()==Qt::RightButton) {
-    event->accept();
-    if(numHedgehogs > 1) {
-      numHedgehogs--;
-      pOurFrameTeams->overallHedgehogs--;
-      emit hedgehogsNumChanged();
-    }
-  } else {
-    event->ignore();
-    return;
-  }
-  repaint();
-}
-
 void CHedgehogerWidget::setHHNum(unsigned int num)
 {
-  unsigned int diff=numHedgehogs-num;
-  numHedgehogs=num;
+  unsigned int diff=numItems-num;
+  numItems=num;
   pOurFrameTeams->overallHedgehogs+=diff;
   repaint();
 }
 
-void CHedgehogerWidget::paintEvent(QPaintEvent* event)
-{
-  QImage image(":/res/hh25x25.png");
-
-  QPainter painter(this);
-
-  for(int i=0; i<numHedgehogs; i++) {
-    QRect target(11 * i, i % 2, 25, 25);
-    painter.drawImage(target, image);
-  }
-}
-
 unsigned char CHedgehogerWidget::getHedgehogsNum() const
 {
-  return numHedgehogs;
+  return numItems;
 }
--- a/QTfrontend/hedgehogerWidget.h	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/hedgehogerWidget.h	Mon Sep 24 20:34:28 2007 +0000
@@ -19,17 +19,17 @@
 #ifndef _HEDGEHOGER_WIDGET
 #define _HEDGEHOGER_WIDGET
 
-#include <QWidget>
+#include "itemNum.h"
 
 class FrameTeams;
 
-class CHedgehogerWidget : public QWidget
+class CHedgehogerWidget : public ItemNum
 {
   Q_OBJECT
 
  public:
-  CHedgehogerWidget(QWidget * parent);
-  ~CHedgehogerWidget();
+  CHedgehogerWidget(const QImage& im, QWidget * parent);
+  virtual ~CHedgehogerWidget();
   unsigned char getHedgehogsNum() const;
   void setHHNum (unsigned int num);
   void setNonInteractive();
@@ -38,14 +38,12 @@
   void hedgehogsNumChanged();
 
  protected:
-  virtual void paintEvent(QPaintEvent* event);
-  virtual void mousePressEvent ( QMouseEvent * event );
+  virtual void incItems();
+  virtual void decItems();
 
  private:
   CHedgehogerWidget();
-  unsigned char numHedgehogs;
   FrameTeams* pOurFrameTeams;
-  bool nonInteractive;
 };
 
 #endif // _HEDGEHOGER_WIDGET
--- a/QTfrontend/hedgewars.pro	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/hedgewars.pro	Mon Sep 24 20:34:28 2007 +0000
@@ -42,7 +42,8 @@
            SDLs.h \
            playrecordpage.h \
            hwconsts.h \
-           selectWeapon.h
+           selectWeapon.h \
+           itemNum.h
            
            
 SOURCES += binds.cpp \
@@ -75,7 +76,8 @@
            SDLs.cpp \
            playrecordpage.cpp \
            hwconsts.cpp \
-           selectWeapon.cpp
+           selectWeapon.cpp \
+           itemNum.cpp
 
 TRANSLATIONS += ../share/hedgewars/Data/Locale/hedgewars_ru.ts
 
--- a/QTfrontend/hwform.cpp	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/hwform.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -44,7 +44,6 @@
 #include "netudpwidget.h"
 #include "chatwidget.h"
 #include "playrecordpage.h"
-#include "selectWeapon.h"
 
 HWForm::HWForm(QWidget *parent)
   : QMainWindow(parent), pnetserver(0), pUdpServer(0), editedTeam(0)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/itemNum.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -0,0 +1,57 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+ 
+#include "itemNum.h"
+
+#include <QMouseEvent>
+#include <QPainter>
+
+ItemNum::ItemNum(const QImage& im, QWidget * parent) :
+    m_im(im), QWidget(parent), nonInteractive(false)
+{
+}
+
+ItemNum::~ItemNum()
+{
+}
+
+void ItemNum::mousePressEvent ( QMouseEvent * event )
+{
+  if(nonInteractive) return;
+  if(event->button()==Qt::LeftButton) {
+    event->accept();
+    incItems();
+  } else if (event->button()==Qt::RightButton) {
+    event->accept();
+    decItems();
+  } else {
+    event->ignore();
+    return;
+  }
+  repaint();
+}
+
+void ItemNum::paintEvent(QPaintEvent* event)
+{
+  QPainter painter(this);
+
+  for(int i=0; i<numItems; i++) {
+    QRect target(11 * i, i % 2, 25, 25);
+    painter.drawImage(target, m_im);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/itemNum.h	Mon Sep 24 20:34:28 2007 +0000
@@ -0,0 +1,48 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+ 
+#include <QWidget>
+#include <QImage>
+
+#ifndef _ITEM_NUM_INCLUDED
+#define _ITEM_NUM_INCLUDED
+
+class ItemNum : public QWidget
+{
+  Q_OBJECT
+
+  protected:
+    ItemNum(const QImage& im, QWidget * parent);
+    virtual ~ItemNum()=0;
+    
+    bool nonInteractive;
+    unsigned char numItems;
+    
+    // from QWidget
+    virtual void mousePressEvent ( QMouseEvent * event );
+    virtual void paintEvent(QPaintEvent* event);
+  
+    // to be implemented in child
+    virtual void incItems()=0;
+    virtual void decItems()=0;
+    
+  private:
+    QImage m_im;
+};
+
+#endif // _ITEM_NUM_INCLUDED
--- a/QTfrontend/pages.cpp	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/pages.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -43,6 +43,7 @@
 #include "chatwidget.h"
 #include "SDLs.h"
 #include "playrecordpage.h"
+#include "selectWeapon.h"
 
 PageMain::PageMain(QWidget* parent) : QWidget(parent)
 {
@@ -590,9 +591,12 @@
 	QFont * font14 = new QFont("MS Shell Dlg", 14);
 	QGridLayout * pageLayout = new QGridLayout(this);
 	pageLayout->setMargin(25);
-	pageLayout->setColumnStretch(0, 1);
-	pageLayout->setColumnStretch(1, 2);
-	pageLayout->setColumnStretch(2, 1);
+	//pageLayout->setColumnStretch(0, 1);
+	//pageLayout->setColumnStretch(1, 2);
+	//pageLayout->setColumnStretch(2, 1);
+        
+        pWeapons=new SelWeaponWidget(this);
+	pageLayout->addWidget(pWeapons, 0, 0);
 
 	BtnBack = new QPushButton(this);
 	BtnBack->setFont(*font14);
--- a/QTfrontend/pages.h	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/pages.h	Mon Sep 24 20:34:28 2007 +0000
@@ -42,6 +42,7 @@
 class HWNetUdpWidget;
 class QTextEdit;
 class HWChatWidget;
+class SelWeaponWidget;
 
 class PageMain : public QWidget
 {
@@ -235,6 +236,7 @@
 	PageSelectWeapon(QWidget* parent = 0);
 
 	QPushButton *BtnBack;
+        SelWeaponWidget* pWeapons;
 };
 
 #endif // PAGES_H
--- a/QTfrontend/selectWeapon.cpp	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/selectWeapon.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -1,1 +1,24 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
 #include "selectWeapon.h"
+
+SelWeaponWidget::SelWeaponWidget(QWidget* parent) :
+QWidget(parent)
+{
+}
--- a/QTfrontend/selectWeapon.h	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/selectWeapon.h	Mon Sep 24 20:34:28 2007 +0000
@@ -1,3 +1,21 @@
+/*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
 #ifndef _SELECT_WEAPON_INCLUDED
 #define _SELECT_WEAPON_INCLUDED
 
@@ -6,6 +24,8 @@
 class SelWeaponWidget : public QWidget
 {
   Q_OBJECT
+  public:
+    SelWeaponWidget(QWidget* parent=0);
 };
 
 #endif // _SELECT_WEAPON_INCLUDED
--- a/QTfrontend/teamselhelper.cpp	Sat Sep 22 17:50:14 2007 +0000
+++ b/QTfrontend/teamselhelper.cpp	Mon Sep 24 20:34:28 2007 +0000
@@ -68,8 +68,7 @@
     connect(colorButt, SIGNAL(clicked()), this, SLOT(changeTeamColor()));
     mainLayout.addWidget(colorButt);
 
-    // hedgehogs num
-    phhoger=new CHedgehogerWidget(this);
+    phhoger=new CHedgehogerWidget(QImage(":/res/hh25x25.png"), this);
     connect(phhoger, SIGNAL(hedgehogsNumChanged()), this, SLOT(hhNumChanged()));
     mainLayout.addWidget(phhoger);
   }