Skip to content

Commit

Permalink
player: add button class
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed May 21, 2013
1 parent bfa2943 commit 4c0fd12
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
107 changes: 107 additions & 0 deletions examples/simpleplayer/Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,107 @@
#include "Button.h"

Button::Button(QWidget *parent) :
QPushButton(parent)
, mState(NotFocused)
{
}

Button::Button(const QString& text, QWidget *parent) :
QPushButton(text, parent)
, mState(NotFocused)
{
}

Button::~Button()
{
}

Button::IconState Button::iconState() const
{
return mState;
}

void Button::setIconState(IconState state, bool force)
{
if (mState == state && !force)
return;
mState = state;
setIcon(iconForState(mState));
}

QIcon Button::iconForState(IconState state) const
{
if (!mIcons.contains(state))
return mIcons.begin().value();
return mIcons.value(state);;
}

void Button::setIconForState(IconState state, const QIcon &icon)
{
mIcons.insert(state, icon);
}

void Button::setIconWithSates(const QPixmap &pixmap, IconState s1, IconState s2, IconState s3, IconState s4)
{
int a = pixmap.width();
int b = pixmap.height();
int count = qMax(a, b)/qMin(a, b);
bool hor = pixmap.height() < pixmap.width();
QPoint dp;
if (hor) {
a /= count;
dp.setX(a);
} else {
b /= count;
dp.setY(b);
}
QRect r(0, 0, a, b);
setIconForState(s1, pixmap.copy(r));
if (count > 1) {
r.translate(dp);
setIconForState(s2, pixmap.copy(r));
if (count > 2) {
r.translate(dp);
setIconForState(s3, pixmap.copy(r));
if (count > 3) {
r.translate(dp);
setIconForState(s4, pixmap.copy(r));
}
}
}
setIconState(iconState(), true); //TODO: other states set to existing icon
}

void Button::enterEvent(QEvent *e)
{
QPushButton::enterEvent(e);
if (mIcons.isEmpty())
return;
setIconState(Focused);
emit entered();
}

void Button::leaveEvent(QEvent *e)
{
QPushButton::leaveEvent(e);
if (mIcons.isEmpty())
return;
setIconState(NotFocused);
emit leaved();
}

void Button::mousePressEvent(QMouseEvent *e)
{
QPushButton::mousePressEvent(e);
if (mIcons.isEmpty())
return;
setIconState(Pressed);
}

void Button::mouseReleaseEvent(QMouseEvent *e)
{
QPushButton::mouseReleaseEvent(e);
if (mIcons.isEmpty())
return;
setIconState(Focused);
}
48 changes: 48 additions & 0 deletions examples/simpleplayer/Button.h
Original file line number Diff line number Diff line change
@@ -0,0 1,48 @@
#ifndef BUTTON_H
#define BUTTON_H

//TODO: on/off, like qicon

#include <QPushButton>
#include <QMap>

class Button : public QPushButton
{
Q_OBJECT
public:
//like QIcon::Mode
//use icon state to avoid setIcon with the same icon
enum IconState {
Disabled, //inactive
Pressed, //focused and pressed
Focused, //mouse enter
NotFocused //mouse leave
};

explicit Button(QWidget *parent = 0);
explicit Button(const QString& text, QWidget *parent = 0);
virtual ~Button();
IconState iconState() const;
void setIconState(IconState state, bool force = false);
QIcon iconForState(IconState state) const;
void setIconForState(IconState state, const QIcon& icon);

void setIconWithSates(const QPixmap& pixmap, IconState s1 = NotFocused, IconState s2 = Disabled
, IconState s3 = Pressed, IconState s4 = Focused);
signals:
void entered();
void leaved();

protected:
//for change icon.
virtual void enterEvent(QEvent *);
virtual void leaveEvent(QEvent *);
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseReleaseEvent(QMouseEvent *e);

private:
IconState mState;
QMap<IconState, QIcon> mIcons;
};

#endif // BUTTON_H
3 changes: 3 additions & 0 deletions examples/simpleplayer/simpleplayer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 9,14 @@ preparePaths($$OUT_PWD/../../out)

SOURCES = main.cpp \
MainWindow.cpp \
Button.cpp \
Slider.cpp

HEADERS = \
MainWindow.h \
Button.h \
Slider.h


include($$PROJECTROOT/deploy.pri)

0 comments on commit 4c0fd12

Please sign in to comment.