forked from myst6re/hyne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartWidget.cpp
More file actions
133 lines (110 loc) · 3.71 KB
/
StartWidget.cpp
File metadata and controls
133 lines (110 loc) · 3.71 KB
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
/****************************************************************************
** Hyne Final Fantasy VIII Save Editor
** Copyright (C) 2009-2013 Arzel Jérôme <myst6re@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "StartWidget.h"
#include "SavecardView.h"
StartWidget::StartWidget(QWidget *parent) :
QWidget(parent), _cursorPosition(-1)
{
setMouseTracking(true);
}
void StartWidget::addAction(QAction *action)
{
QWidget::addAction(action);
}
QAction *StartWidget::addAction(const QString &text)
{
QAction *act = new QAction(text, this);
addAction(act);
return act;
}
QAction *StartWidget::addAction(const QString &text, const QObject *receiver, const char *method)
{
QAction *act = addAction(text);
connect(act, SIGNAL(triggered()), receiver, method);
return act;
}
void StartWidget::setCursorPosition(int actionID)
{
_cursorPosition = actionID;
update(actionsPosition().x()-38, actionsPosition().y()+12, 48, 30 + actions().size() * OPTION_HEIGHT);
}
int StartWidget::actionID(const QPoint &pos) const
{
if(pos.x() < actionsPosition().x()
|| pos.x() >= actionsPosition().x() + sizeHint().width()
|| pos.y() < actionsPosition().y()
|| pos.y() >= actionsPosition().y() + sizeHint().height())
return -1;
return (pos.y() - actionsPosition().y()) / OPTION_HEIGHT;
}
void StartWidget::paintEvent(QPaintEvent *)
{
if(actions().isEmpty()) return;
QRegExp remAnd("&([^&])");
bool enabledState = actions().first()->isEnabled();
int actionID = 0;
QPainter painter(this);
painter.setBrush(Qt::black);
painter.drawRect(rect());
painter.translate(actionsPosition());
painter.setBrush(QPixmap(QString(":/images/menu-fond%1.png").arg(enabledState ? "" : "2")));
foreach(const QAction *act, actions()) {
if(act->isEnabled() != enabledState) {
enabledState = act->isEnabled();
painter.setBrush(QPixmap(QString(":/images/menu-fond%1.png").arg(enabledState ? "" : "2")));
}
QString text = act->text();
text.replace(remAnd, "\\1");
SavecardView::drawFrame(&painter, OPTION_WIDTH, OPTION_HEIGHT);
FF8Text::drawTextArea(&painter, QPoint(12, 12), text);
if(actionID == _cursorPosition) {
painter.drawPixmap(-38, 12, QPixmap(":/images/cursor.png"));
}
painter.translate(0, OPTION_HEIGHT);
++actionID;
}
painter.end();
}
void StartWidget::mouseMoveEvent(QMouseEvent *event)
{
int actionID = this->actionID(event->pos());
if(actionID >= 0 && actionID < actions().size()) {
QAction *act = actions().at(actionID);
if(act->isEnabled()) {
act->hover();
setCursorPosition(actionID);
} else {
setCursorPosition(-1);
}
}
}
void StartWidget::mouseReleaseEvent(QMouseEvent *event)
{
int actionID = this->actionID(event->pos());
if(actionID >= 0 && actionID < actions().size()) {
QAction *act = actions().at(actionID);
if(act->isEnabled()) {
act->trigger();
emit actionTriggered(act);
}
}
}
void StartWidget::leaveEvent(QEvent *)
{
setCursorPosition(-1);
}