summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNorwegian Rock Cat <qt-info@nokia.com>2009-05-08 17:40:16 +0200
committerNorwegian Rock Cat <qt-info@nokia.com>2009-05-08 17:40:16 +0200
commit33b94de2223307f5daefb88f6c9641421acd5135 (patch)
tree431b87925369c81e4be0918f5dea9372d35283e2 /src
parent90a1904583125274ec5f9561352e8d5419ae3cfd (diff)
Some infrastructure for painting.
Diffstat (limited to 'src')
-rw-r--r--src/qtsegmentcontrol.cpp74
-rw-r--r--src/qtsegmentcontrol.h2
2 files changed, 75 insertions, 1 deletions
diff --git a/src/qtsegmentcontrol.cpp b/src/qtsegmentcontrol.cpp
index c3918b8..39e5740 100644
--- a/src/qtsegmentcontrol.cpp
+++ b/src/qtsegmentcontrol.cpp
@@ -1,9 +1,14 @@
#include <QtGui/QIcon>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
+#include <QtGui/QStyleOption>
#include "qtsegmentcontrol.h"
+#ifdef Q_WS_MAC
+#include <Carbon/Carbon.h>
+#endif
+
struct SegmentInfo {
SegmentInfo() : menu(0), selected(false), enabled(true) {}
~SegmentInfo() { delete menu; }
@@ -29,6 +34,36 @@ public:
inline bool indexOK(int index) { return index >= 0 && index < segments.count(); }
};
+class QtStyleOptionSegmentControlSegment : public QStyleOption
+{
+public:
+ enum StyleOptionType { Type = 100000 };
+ enum StyleOptionVersion { Version = 1 };
+
+ enum SegmentPosition { Beginning, Middle, End, OnlyOneSegment };
+ enum SelectedPosition { NotAdjacent, NextIsSelected, PreviousIsSelected };
+
+ QString text;
+ QIcon icon;
+ QSize iconSize;
+ SegmentPosition position;
+ SelectedPosition selectedPosition;
+
+ QtStyleOptionSegmentControlSegment()
+ : position(OnlyOneSegment), selectedPosition(NotAdjacent) { }
+ QtStyleOptionSegmentControlSegment(const QtStyleOptionSegmentControlSegment &other)
+ : QStyleOption(Version, Type) { *this = other; }
+
+protected:
+ QtStyleOptionSegmentControlSegment(int version);
+};
+
+static void drawSegmentControlSegment(const QStyleOption *option,
+ QPainter *painter, QWidget *widget = 0)
+{
+ painter->fillRect(option->rect, Qt::blue);
+}
+
QtSegmentControl::QtSegmentControl(QWidget *parent)
: QWidget(parent), d(new QtSegmentControlPrivate(this))
{
@@ -204,7 +239,12 @@ QSize QtSegmentControl::sizeHint() const
void QtSegmentControl::paintEvent(QPaintEvent *)
{
QPainter p(this);
- p.fillRect(rect(), Qt::blue);
+ QtStyleOptionSegmentControlSegment segmentInfo;
+ const int segmentCount = d->segments.count();
+ for (int i = 0; i < segmentCount; ++i) {
+ initStyleOption(i, &segmentInfo);
+ drawSegmentControlSegment(&segmentInfo, &p, this);
+ }
}
void QtSegmentControl::mousePressEvent(QMouseEvent *event)
@@ -237,3 +277,35 @@ bool QtSegmentControl::event(QEvent *event)
return QWidget::event(event);
}
+void QtSegmentControl::initStyleOption(int segment, QStyleOption *option)
+{
+ if (!option || !d->indexOK(segment))
+ return;
+ option->initFrom(this);
+ if (QtStyleOptionSegmentControlSegment *sgi = qstyleoption_cast<QtStyleOptionSegmentControlSegment *>(option)) {
+ sgi->iconSize = d->iconSize;
+ const SegmentInfo &segmentInfo = d->segments[segment];
+ if (d->segments.count() == 1) {
+ sgi->position = QtStyleOptionSegmentControlSegment::OnlyOneSegment;
+ } else if (segment == 0) {
+ sgi->position = QtStyleOptionSegmentControlSegment::Beginning;
+ } else if (segment == d->segments.count() - 1) {
+ sgi->position = QtStyleOptionSegmentControlSegment::End;
+ } else {
+ sgi->position = QtStyleOptionSegmentControlSegment::Middle;
+ }
+ if (segmentInfo.selected) {
+ sgi->state |= QStyle::State_Selected;
+ } else {
+ if (d->indexOK(segment - 1) && d->segments[segment - 1].selected) {
+ sgi->selectedPosition = QtStyleOptionSegmentControlSegment::PreviousIsSelected;
+ } else if (d->indexOK(segment + 1) && d->segments[segment + 1].selected) {
+ sgi->selectedPosition = QtStyleOptionSegmentControlSegment::NextIsSelected;
+ } else {
+ sgi->selectedPosition = QtStyleOptionSegmentControlSegment::NotAdjacent;
+ }
+ }
+ sgi->text = segmentInfo.text;
+ sgi->icon = segmentInfo.icon;
+ }
+}
diff --git a/src/qtsegmentcontrol.h b/src/qtsegmentcontrol.h
index 6a8e787..d820f17 100644
--- a/src/qtsegmentcontrol.h
+++ b/src/qtsegmentcontrol.h
@@ -4,6 +4,7 @@
#include <QtGui/QWidget>
class QMenu;
+class QStyleOption;
class QtSegmentControlPrivate;
@@ -56,6 +57,7 @@ public:
QSize sizeHint() const;
protected:
+ void initStyleOption(int segment, QStyleOption *option);
void paintEvent(QPaintEvent *pe);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);