-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get sub-button pixmap from a picture-button ,which has all button-state
- Loading branch information
1 parent
51df436
commit d3e33c3
Showing
14 changed files
with
380 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2016-03-14T20:30:43 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = QwidgetTest | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
widget.cpp \ | ||
skinbutton.cpp \ | ||
resoure.cpp | ||
|
||
HEADERS += widget.h \ | ||
skinbutton.h \ | ||
resoure.h | ||
|
||
RESOURCES += \ | ||
res.qrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "widget.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
Widget w; | ||
//w.setFixedSize(300, 300); | ||
w.show(); | ||
|
||
return a.exec(); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<RCC> | ||
<qresource prefix="/res"> | ||
<file>timeout.bmp</file> | ||
<file>timein.bmp</file> | ||
<file>stop.bmp</file> | ||
<file>play.bmp</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include "resoure.h" | ||
|
||
#define RES_PLAY ":/res/play.bmp" | ||
#define RES_STOP ":/res/stop.bmp" | ||
|
||
#define RES_TIME_IN ":/res/timein.bmp" | ||
#define RES_TIME_OUT ":/res/timeout.bmp" | ||
|
||
namespace Resource { | ||
|
||
class IndexPixmap::Impl | ||
{ | ||
public: | ||
qint32 m_width; | ||
QHash<qint32, QPixmap> m_pixs; | ||
}; | ||
|
||
IndexPixmap::IndexPixmap() | ||
: m_pimpl(new Impl()) | ||
{ | ||
m_pimpl->m_width = 0; | ||
} | ||
|
||
IndexPixmap::IndexPixmap(const char *fileName, quint32 cnt) | ||
: QPixmap(fileName) | ||
, m_pimpl(new Impl()) | ||
{ | ||
m_pimpl->m_width = width() / cnt; | ||
} | ||
|
||
void IndexPixmap::setWidth(quint32 w) | ||
{ | ||
m_pimpl->m_width = w; | ||
} | ||
|
||
QPixmap IndexPixmap::getSubPixmap(qint32 index) const | ||
{ | ||
if (index == -1) | ||
return *this; | ||
|
||
if (!m_pimpl->m_pixs.contains(index)) | ||
m_pimpl->m_pixs.insert(index, copy(index * m_pimpl->m_width, 0, m_pimpl->m_width, height())); | ||
|
||
return m_pimpl->m_pixs.value(index); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
class PixmapManager::Impl | ||
{ | ||
public: | ||
QPixmap getPixmap(IndexPixmapType t, qint32 idx); | ||
|
||
private: | ||
void loadPixmap(IndexPixmapType t); | ||
|
||
private: | ||
typedef QHash<IndexPixmapType, IndexPixmap> IndexPixmapSet; | ||
|
||
IndexPixmapSet m_indexPixmaps; | ||
}; | ||
|
||
QPixmap PixmapManager::Impl::getPixmap(IndexPixmapType t, qint32 idx) | ||
{ | ||
if (!m_indexPixmaps.contains(t)) | ||
loadPixmap(t); | ||
|
||
return m_indexPixmaps.value(t).getSubPixmap(idx); | ||
} | ||
|
||
void PixmapManager::Impl::loadPixmap(IndexPixmapType t) | ||
{ | ||
IndexPixmap tmp; | ||
switch (t) { | ||
case Play: | ||
tmp = IndexPixmap(RES_PLAY, 4); | ||
break; | ||
case Stop: | ||
tmp = IndexPixmap(RES_STOP, 4); | ||
break; | ||
case TimeIn: | ||
tmp = IndexPixmap(RES_TIME_IN, 4); | ||
break; | ||
case TimeOut: | ||
tmp = IndexPixmap(RES_TIME_OUT, 4); | ||
break; | ||
} | ||
|
||
m_indexPixmaps.insert(t, tmp); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
PixmapManager::PixmapManager() | ||
: m_pImpl(new PixmapManager::Impl()) | ||
{ | ||
|
||
} | ||
|
||
PixmapManager &PixmapManager::inst() | ||
{ | ||
static PixmapManager mgr; | ||
return mgr; | ||
} | ||
|
||
QPixmap PixmapManager::getPixmap(IndexPixmapType t, qint32 idx) | ||
{ | ||
return m_pImpl->getPixmap(t, idx); | ||
} | ||
|
||
} // namespace Resource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef RESOURE_H | ||
#define RESOURE_H | ||
|
||
#include <QScopedPointer> | ||
#include <QPixmap> | ||
|
||
namespace Resource { | ||
|
||
typedef enum{ | ||
Play, | ||
Stop, | ||
TimeIn, | ||
TimeOut | ||
}IndexPixmapType; | ||
|
||
class IndexPixmap : public QPixmap | ||
{ | ||
public: | ||
IndexPixmap(); | ||
IndexPixmap(const char *fileName, quint32 cnt = 1); | ||
~IndexPixmap() {} | ||
|
||
public: | ||
void setWidth(quint32 w); | ||
QPixmap getSubPixmap(qint32 index = -1) const; | ||
|
||
private: | ||
class Impl; | ||
QSharedPointer<Impl> m_pimpl; | ||
}; | ||
|
||
|
||
class PixmapManager | ||
{ | ||
public: | ||
PixmapManager(); | ||
|
||
public: | ||
static PixmapManager &inst(); | ||
|
||
public: | ||
QPixmap getPixmap(IndexPixmapType t, qint32 idx = -1); | ||
|
||
private: | ||
class Impl; | ||
QScopedPointer<Impl> m_pImpl; | ||
}; | ||
|
||
} // namespace Resource | ||
|
||
#endif // RESOURE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <QPainter> | ||
#include "skinbutton.h" | ||
#include "resoure.h" | ||
#include <QDebug> | ||
|
||
SkinButton::SkinButton(qint32 type, QWidget *parent) | ||
: QPushButton(parent) | ||
, m_type(type) | ||
, m_index(0) | ||
, m_state(ButtonNormal) | ||
{ | ||
//connect(this, SIGNAL(clicked(bool)), this, SLOT(setIndex(bool))); | ||
} | ||
|
||
void SkinButton::setIndex(bool checked) | ||
{ | ||
qDebug() << "OOOOOOOOOOOOOOOOOOOOO " << checked; | ||
m_index = checked ? 1 : 0; | ||
} | ||
|
||
#if 0 | ||
void SkinButton::setIndex(qint8 index) | ||
{ | ||
m_index = index; | ||
} | ||
#endif | ||
|
||
QSize SkinButton::sizeHint() const | ||
{ | ||
return getStatePixmap().size(); | ||
} | ||
|
||
void SkinButton::enterEvent(QEvent *event) | ||
{ | ||
m_state = ButtonHovered; | ||
QPushButton::enterEvent(event); | ||
} | ||
|
||
void SkinButton::leaveEvent(QEvent *event) | ||
{ | ||
m_state = ButtonNormal; | ||
QPushButton::leaveEvent(event); | ||
} | ||
|
||
void SkinButton::mousePressEvent(QMouseEvent *event) | ||
{ | ||
m_state = ButtonPressed; | ||
QPushButton::mousePressEvent(event); | ||
} | ||
|
||
void SkinButton::mouseReleaseEvent(QMouseEvent *event) | ||
{ | ||
m_state = ButtonHovered; | ||
QPushButton::mouseReleaseEvent(event); | ||
} | ||
|
||
void SkinButton::paintEvent(QPaintEvent *) | ||
{ | ||
QPainter ptr(this); | ||
ptr.drawPixmap(0, 0, width(), height(), getStatePixmap()); | ||
} | ||
|
||
QPixmap SkinButton::getStatePixmap() const | ||
{ | ||
qDebug() << "HHHHHHHHHHHHHHHH m_index , m_state " << m_index << m_state; | ||
return Resource::PixmapManager::inst().getPixmap( | ||
(Resource::IndexPixmapType)m_type, /*m_index * 4 + */m_state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef SKINBUTTON_H | ||
#define SKINBUTTON_H | ||
|
||
#include <QPushButton> | ||
|
||
class SkinButton : public QPushButton | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
typedef enum{ | ||
ButtonNormal = 0, | ||
ButtonHovered = 1, | ||
ButtonPressed = 2, | ||
ButtonDisabeled = 3, | ||
}ButtonState; | ||
|
||
public: | ||
explicit SkinButton(qint32 type, QWidget *parent = 0); | ||
~SkinButton() {} | ||
|
||
public slots: | ||
void setIndex(bool checked); | ||
//void setIndex(qint8 index); | ||
|
||
|
||
protected: | ||
|
||
QSize sizeHint() const; | ||
void enterEvent(QEvent *); | ||
void leaveEvent(QEvent *); | ||
|
||
void mousePressEvent(QMouseEvent *); | ||
void mouseReleaseEvent(QMouseEvent *); | ||
|
||
void paintEvent(QPaintEvent *); | ||
|
||
private: | ||
QPixmap getStatePixmap() const; | ||
|
||
private: | ||
qint32 m_type; | ||
qint8 m_index; | ||
ButtonState m_state; | ||
}; | ||
|
||
#endif // SKINBUTTON_H |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "widget.h" | ||
|
||
#include <QDebug> | ||
#include "resoure.h" | ||
#include <QHBoxLayout> | ||
|
||
Widget::Widget(QWidget *parent) | ||
: QWidget(parent) | ||
, m_button(0) | ||
{ | ||
QHBoxLayout *layout = new QHBoxLayout(this); | ||
m_button = new SkinButton(Resource::Play); | ||
|
||
SkinButton *stopBtn = new SkinButton(Resource::Stop); | ||
SkinButton *timeInBtn = new SkinButton(Resource::TimeIn); | ||
SkinButton *timeOutBtn = new SkinButton(Resource::TimeOut); | ||
//SkinButton button(Resource::Play, this); | ||
layout->addWidget(m_button); | ||
layout->addWidget(stopBtn); | ||
layout->addWidget(timeInBtn); | ||
layout->addWidget(timeOutBtn); | ||
} | ||
|
||
Widget::~Widget() | ||
{ | ||
|
||
} | ||
|
||
void Widget::keyPressEvent(QKeyEvent *event) | ||
{ | ||
qDebug() << "HHHHHHHHHHHHHHHHHHH"; | ||
QWidget::keyPressEvent(event); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef WIDGET_H | ||
#define WIDGET_H | ||
|
||
#include <QWidget> | ||
#include <QKeyEvent> | ||
#include "skinbutton.h" | ||
|
||
class Widget : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
Widget(QWidget *parent = 0); | ||
~Widget(); | ||
|
||
protected: | ||
void keyPressEvent(QKeyEvent *); | ||
|
||
private: | ||
SkinButton *m_button; | ||
}; | ||
|
||
#endif // WIDGET_H |