-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.cpp
135 lines (118 loc) · 4.16 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information ([email protected])
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include <QtGui>
#include <QtWebKit>
#include "flickcharm.h"
#define ITEM_WIDTH 300
#define ITEM_HEIGHT 30
class TextItem: public QGraphicsItem
{
public:
TextItem(const QString &str) {
QStringList list = str.split(' ');
str1 = list[0];
str2 = list[1];
font1 = QFont("Arial");
font2 = QFont("Arial");
font1.setBold(true);
font1.setPixelSize(ITEM_HEIGHT / 2);
font2.setPixelSize(ITEM_HEIGHT / 2);
offset = QFontMetrics(font1).width(str1) + 15;
}
QRectF boundingRect() const {
return QRectF(0, 0, ITEM_WIDTH, ITEM_HEIGHT);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget*) {
if (option->state & QStyle::State_Selected) {
painter->fillRect(boundingRect(), QColor(0, 128, 240));
painter->setPen(Qt::white);
} else {
painter->setPen(Qt::lightGray);
painter->drawRect(boundingRect());
painter->setPen(Qt::black);
}
painter->setFont(font1);
painter->drawText(QRect(10, 0, offset, ITEM_HEIGHT), Qt::AlignVCenter, str1);
painter->setFont(font2);
painter->drawText(QRect(offset, 0, ITEM_WIDTH, ITEM_HEIGHT), Qt::AlignVCenter, str2);
}
private:
QFont font1;
QFont font2;
QString str1;
QString str2;
int offset;
};
// Returns a list of two-word color names
static QStringList colorPairs(int max)
{
// capitalize the first letter
QStringList colors = QColor::colorNames();
colors.removeAll("transparent");
int num = colors.count();
for (int c = 0; c < num; ++c)
colors[c] = colors[c][0].toUpper() + colors[c].mid(1);
// combine two colors, e.g. "lime skyblue"
QStringList combinedColors;
for (int i = 0; i < num; ++i)
for (int j = 0; j < num; ++j)
combinedColors << QString("%1 %2").arg(colors[i]).arg(colors[j]);
// randomize it
colors.clear();
while (combinedColors.count()) {
int i = qrand() % combinedColors.count();
colors << combinedColors[i];
combinedColors.removeAt(i);
if (colors.count() == max)
break;
}
return colors;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStringList colors = colorPairs(5000);
QGraphicsScene scene;
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
for (int i = 0; i < colors.count(); ++i) {
TextItem *item = new TextItem(colors[i]);
scene.addItem(item);
item->setPos(0, i*ITEM_HEIGHT);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
}
scene.setItemIndexMethod(QGraphicsScene::BspTreeIndex);
QGraphicsView canvas;
canvas.setScene(&scene);
canvas.setRenderHints(QPainter::TextAntialiasing);
canvas.setFrameShape(QFrame::NoFrame);
canvas.setWindowTitle("Flickable Canvas");
canvas.show();
QWebView web;
web.setUrl(QUrl("http://news.google.com"));
web.setWindowTitle("Flickable Web View");
web.show();
FlickCharm FlickCharm;
FlickCharm.activateOn(&canvas);
FlickCharm.activateOn(&web);
return app.exec();
}