Custom button class added.
authorJukka K <jktla@jktla-desktop.(none)>
Thu, 22 Apr 2010 13:43:08 +0000 (16:43 +0300)
committerJukka K <jktla@jktla-desktop.(none)>
Thu, 22 Apr 2010 13:43:08 +0000 (16:43 +0300)
Client/custombutton.cpp [new file with mode: 0644]
Client/custombutton.h [new file with mode: 0644]

diff --git a/Client/custombutton.cpp b/Client/custombutton.cpp
new file mode 100644 (file)
index 0000000..af023ad
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Custom button class for customized button.
+ *
+ * @author      Jukka Kurttila <jktla@suomi24.fi>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#include "custombutton.h"
+
+#include <QPainter>
+#include <QIcon>
+
+/**
+  *Constructor of this class.
+  */
+CustomButton::CustomButton(QWidget *parent, QIcon *icon) : QWidget(parent)
+{
+    bPressed = false;
+    if( icon )
+    {
+        pict1 = new QPixmap(icon->pixmap(125,125,QIcon::Normal,QIcon::On));
+        pict2 = new QPixmap(icon->pixmap(125,125,QIcon::Normal,QIcon::Off));
+    }
+}
+/**
+  *Destructor of this class.
+  */
+CustomButton::~CustomButton()
+{
+    if(!pict1)
+        delete pict1;
+    if(!pict2)
+        delete pict2;
+}
+
+void CustomButton::paintEvent(QPaintEvent *)
+{
+    QPainter painter(this);
+
+    if(!bPressed)
+        painter.drawPixmap(0,0,*pict2);
+    else
+        painter.drawPixmap(0,0,*pict1);
+
+    //Debug print
+    //painter.drawText(50,50,"y: "+QString::number(mY));
+}
+void CustomButton::mousePressEvent(QMouseEvent* me)
+{
+    bPressed = true;
+    repaint();
+}
+void CustomButton::mouseReleaseEvent(QMouseEvent* me)
+{
+    mX = me->x();
+    mY = me->y();
+    //Emit open dialog signal if mouse is still over button
+    if( mY < this->height() && mY > 0 && mX < this->width() && mX > 0 )
+        emit OpenDialog();
+
+    bPressed = false;
+    repaint();
+}
+void CustomButton::mouseMoveEvent(QMouseEvent* me)
+{
+    mX = me->x();
+    mY = me->y();
+    //Is mouse moved outside button?
+    if( mY > this->height() || mY < 0 || mX > this->width() || mX < 0 )
+        bPressed = false;
+    else
+        bPressed = true;
+    repaint();
+}
diff --git a/Client/custombutton.h b/Client/custombutton.h
new file mode 100644 (file)
index 0000000..cad74ec
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Custom button class for customized button.
+ *
+ * @author      Jukka Kurttila <jktla@suomi24.fi>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#ifndef CUSTOMBUTTON_H
+#define CUSTOMBUTTON_H
+
+#include <QPushButton>
+#include <QPixmap>
+#include <QMouseEvent>
+
+class CustomButton : public QWidget
+{
+    Q_OBJECT
+public:
+    CustomButton(QWidget *parent = 0, QIcon* iconParam = 0);
+    ~CustomButton();
+
+signals:
+    void OpenDialog();
+
+protected:
+    void paintEvent(QPaintEvent *);
+    void mousePressEvent(QMouseEvent* me);
+    void mouseReleaseEvent(QMouseEvent* me);
+    void mouseMoveEvent(QMouseEvent* me);
+
+private:
+    QPixmap* pict1;
+    QPixmap* pict2;
+    bool bPressed;
+    int mX,mY;
+};
+
+#endif // CUSTOMBUTTON_H