OSDN Git Service

upgrade to 3.6.1
[jnethack/source.git] / win / Qt4 / qt4icon.cpp
1 // Copyright (c) Warwick Allison, 1999.
2 // Qt4 conversion copyright (c) Ray Chason, 2012-2014.
3 // NetHack may be freely redistributed.  See license for details.
4
5 // qt4icon.cpp -- a labelled icon
6
7 #include "hack.h"
8 #undef Invisible
9 #undef Warning
10 #undef index
11 #undef msleep
12 #undef rindex
13 #undef wizard
14 #undef yn
15 #undef min
16 #undef max
17
18 #include <QtGui/QtGui>
19 #if QT_VERSION >= 0x050000
20 #include <QtWidgets/QtWidgets>
21 #endif
22 #include "qt4icon.h"
23
24 namespace nethack_qt4 {
25
26 NetHackQtLabelledIcon::NetHackQtLabelledIcon(QWidget* parent, const char* l) :
27     QWidget(parent),
28     low_is_good(false),
29     prev_value(-123),
30     turn_count(-1),
31     label(new QLabel(l,this)),
32     icon(0)
33 {
34     initHighlight();
35 }
36
37 NetHackQtLabelledIcon::NetHackQtLabelledIcon(QWidget* parent, const char* l, const QPixmap& i) :
38     QWidget(parent),
39     low_is_good(false),
40     prev_value(-123),
41     turn_count(-1),
42     label(new QLabel(l,this)),
43     icon(new QLabel(this))
44 {
45     setIcon(i);
46     initHighlight();
47 }
48
49 void NetHackQtLabelledIcon::initHighlight()
50 {
51     hl_good = "QLabel { background-color : green; color : white }";
52     hl_bad  = "QLabel { background-color : red  ; color : white }";
53 }
54
55 void NetHackQtLabelledIcon::setLabel(const QString& t, bool lower)
56 {
57     if (!label) {
58         label=new QLabel(this);
59         label->setFont(font());
60         resizeEvent(0);
61     }
62     if (label->text() != t) {
63         label->setText(t);
64         highlight(lower==low_is_good ? hl_good : hl_bad);
65     }
66 }
67
68 void NetHackQtLabelledIcon::setLabel(const QString& t, long v, long cv, const QString& tail)
69 {
70     QString buf;
71     if (v==NoNum) {
72         buf = "";
73     } else {
74         buf.sprintf("%ld", v);
75     }
76     setLabel(t + buf + tail, cv < prev_value);
77     prev_value=cv;
78 }
79
80 void NetHackQtLabelledIcon::setLabel(const QString& t, long v, const QString& tail)
81 {
82     setLabel(t,v,v,tail);
83 }
84
85 void NetHackQtLabelledIcon::setIcon(const QPixmap& i)
86 {
87     if (icon) icon->setPixmap(i);
88     else { icon=new QLabel(this); icon->setPixmap(i); resizeEvent(0); }
89     icon->resize(i.width(),i.height());
90 }
91
92 void NetHackQtLabelledIcon::setFont(const QFont& f)
93 {
94     QWidget::setFont(f);
95     if (label) label->setFont(f);
96 }
97
98 void NetHackQtLabelledIcon::show()
99 {
100 #if QT_VERSION >= 300
101     if (isHidden())
102 #else
103     if (!isVisible())
104 #endif
105         highlight(hl_bad);
106     QWidget::show();
107 }
108
109 QSize NetHackQtLabelledIcon::sizeHint() const
110 {
111     QSize iconsize, textsize;
112
113     if (label && !icon) return label->sizeHint();
114     if (icon && !label) return icon->sizeHint();
115     if (!label && !icon) return QWidget::sizeHint();
116
117     iconsize = icon->sizeHint();
118     textsize = label->sizeHint();
119     return QSize(
120         std::max(iconsize.width(), textsize.width()),
121         iconsize.height() + textsize.height());
122 }
123
124 QSize NetHackQtLabelledIcon::minimumSizeHint() const
125 {
126     QSize iconsize, textsize;
127
128     if (label && !icon) return label->minimumSizeHint();
129     if (icon && !label) return icon->minimumSizeHint();
130     if (!label && !icon) return QWidget::minimumSizeHint();
131
132     iconsize = icon->minimumSizeHint();
133     textsize = label->minimumSizeHint();
134     return QSize(
135         std::max(iconsize.width(), textsize.width()),
136         iconsize.height() + textsize.height());
137 }
138
139 void NetHackQtLabelledIcon::highlightWhenChanging()
140 {
141     turn_count=0;
142 }
143
144 void NetHackQtLabelledIcon::lowIsGood()
145 {
146     low_is_good=true;
147 }
148
149 void NetHackQtLabelledIcon::dissipateHighlight()
150 {
151     if (turn_count>0) {
152         turn_count--;
153         if (!turn_count)
154             unhighlight();
155     }
156 }
157
158 void NetHackQtLabelledIcon::highlight(const QString& hl)
159 {
160     if (label) { // Surely it is?!
161         if (turn_count>=0) {
162             label->setStyleSheet(hl);
163             turn_count=4;
164             // `4' includes this turn, so dissipates after
165             // 3 more keypresses.
166         } else {
167             label->setStyleSheet("");
168         }
169     }
170 }
171
172 void NetHackQtLabelledIcon::unhighlight()
173 {
174     if (label) { // Surely it is?!
175         label->setStyleSheet("");
176     }
177 }
178
179 void NetHackQtLabelledIcon::resizeEvent(QResizeEvent*)
180 {
181     setAlignments();
182
183     //int labw=label ? label->fontMetrics().width(label->text()) : 0;
184     int labh=label ? label->fontMetrics().height() : 0;
185     int icoh=icon ? icon->height() : 0;
186     int h=icoh+labh;
187     int icoy=(h>height() ? height()-labh-icoh : height()/2-h/2);
188     int laby=icoy+icoh;
189     if (icon) {
190         icon->setGeometry(0,icoy,width(),icoh);
191     }
192     if (label) {
193         label->setGeometry(0,laby,width(),labh);
194     }
195 }
196
197 void NetHackQtLabelledIcon::setAlignments()
198 {
199     if (label) label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
200     if (icon) icon->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
201 }
202
203 } // namespace nethack_qt4