OSDN Git Service

upgrade to 3.6.2
[jnethack/source.git] / win / Qt4 / qt4set.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 // qt4set.cpp -- the Qt settings
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 "qt4set.h"
23 #include "qt4set.moc"
24 #include "qt4glyph.h"
25 #include "qt4str.h"
26
27 /* Used by tile/font-size patch below and in ../../src/files.c */
28 char *qt_tilewidth=NULL;
29 char *qt_tileheight=NULL;
30 char *qt_fontsize=NULL;
31 #if defined(QWS)
32 int qt_compact_mode = 1;
33 #else
34 int qt_compact_mode = 0;
35 #endif
36
37 namespace nethack_qt4 {
38
39 #define TILEWMIN 6
40 #define TILEHMIN 6
41
42 NetHackQtSettings::NetHackQtSettings(int w, int h) :
43     settings(),
44     tilewidth(this),
45     tileheight(this),
46     widthlbl("&Width:",this),
47     heightlbl("&Height:",this),
48     whichsize("&Zoomed",this),
49     fontsize(this),
50     normal("times"),
51 #ifdef WS_WIN
52     normalfixed("courier new"),
53 #else
54     normalfixed("courier"),
55 #endif
56     large("times"),
57     theglyphs(0)
58
59 {
60     int default_fontsize;
61
62     widthlbl.setBuddy(&tilewidth);
63     tilewidth.setRange(TILEWMIN, 128);
64     heightlbl.setBuddy(&tileheight);
65     tileheight.setRange(TILEHMIN, 128);
66
67     tilewidth.setValue(settings.value("tilewidth", 16).toInt());
68     tileheight.setValue(settings.value("tileheight", 16).toInt());
69     default_fontsize = settings.value("fontsize", 2).toInt();
70
71     // Tile/font sizes read from .nethackrc
72     if (qt_tilewidth != NULL) {
73         tilewidth.setValue(atoi(qt_tilewidth));
74         delete[] qt_tilewidth;
75     }
76     if (qt_tileheight != NULL) {
77         tileheight.setValue(atoi(qt_tileheight));
78         delete[] qt_tileheight;
79     }
80     if (qt_fontsize != NULL) {
81         switch (tolower(qt_fontsize[0])) {
82           case 'h': default_fontsize = 0; break;
83           case 'l': default_fontsize = 1; break;
84           case 'm': default_fontsize = 2; break;
85           case 's': default_fontsize = 3; break;
86           case 't': default_fontsize = 4; break;
87         }
88         delete[] qt_fontsize;
89     }
90
91     theglyphs=new NetHackQtGlyphs();
92     resizeTiles();
93
94     connect(&tilewidth,SIGNAL(valueChanged(int)),this,SLOT(resizeTiles()));
95     connect(&tileheight,SIGNAL(valueChanged(int)),this,SLOT(resizeTiles()));
96     connect(&whichsize,SIGNAL(toggled(bool)),this,SLOT(setGlyphSize(bool)));
97
98     fontsize.addItem("Huge");
99     fontsize.addItem("Large");
100     fontsize.addItem("Medium");
101     fontsize.addItem("Small");
102     fontsize.addItem("Tiny");
103     fontsize.setCurrentIndex(default_fontsize);
104     connect(&fontsize,SIGNAL(activated(int)),this,SLOT(changedFont()));
105
106     QGridLayout* grid = new QGridLayout(this);
107     grid->addWidget(&whichsize, 0, 0, 1, 2);
108     grid->addWidget(&tilewidth, 1, 1); grid->addWidget(&widthlbl, 1, 0);
109     grid->addWidget(&tileheight, 2, 1); grid->addWidget(&heightlbl, 2, 0);
110     QLabel* flabel=new QLabel("&Font:",this);
111     flabel->setBuddy(&fontsize);
112     grid->addWidget(flabel, 3, 0); grid->addWidget(&fontsize, 3, 1);
113     QPushButton* dismiss=new QPushButton("Dismiss",this);
114     dismiss->setDefault(true);
115     grid->addWidget(dismiss, 4, 0, 1, 2);
116     grid->setRowStretch(4,0);
117     grid->setColumnStretch(1,1);
118     grid->setColumnStretch(2,2);
119     grid->activate();
120
121     connect(dismiss,SIGNAL(clicked()),this,SLOT(accept()));
122     resize(150,140);
123 }
124
125 NetHackQtGlyphs& NetHackQtSettings::glyphs()
126 {
127     return *theglyphs;
128 }
129
130 void NetHackQtSettings::changedFont()
131 {
132     settings.setValue("fontsize", fontsize.currentIndex());
133     emit fontChanged();
134 }
135
136 void NetHackQtSettings::resizeTiles()
137 {
138     int w = tilewidth.value();
139     int h = tileheight.value();
140
141     settings.setValue("tilewidth", tilewidth.value());
142     settings.setValue("tileheight", tileheight.value());
143     theglyphs->setSize(w,h);
144     emit tilesChanged();
145 }
146
147 void NetHackQtSettings::toggleGlyphSize()
148 {
149     whichsize.toggle();
150 }
151
152 void NetHackQtSettings::setGlyphSize(bool which)
153 {
154     QSize n = QSize(tilewidth.value(),tileheight.value());
155     if ( othersize.isValid() ) {
156         tilewidth.blockSignals(true);
157         tileheight.blockSignals(true);
158         tilewidth.setValue(othersize.width());
159         tileheight.setValue(othersize.height());
160         tileheight.blockSignals(false);
161         tilewidth.blockSignals(false);
162         resizeTiles();
163     }
164     othersize = n;
165 }
166
167 const QFont& NetHackQtSettings::normalFont()
168 {
169     static int size[]={ 18, 14, 12, 10, 8 };
170     normal.setPointSize(size[fontsize.currentIndex()]);
171     return normal;
172 }
173
174 const QFont& NetHackQtSettings::normalFixedFont()
175 {
176     static int size[]={ 18, 14, 13, 10, 8 };
177     normalfixed.setPointSize(size[fontsize.currentIndex()]);
178     return normalfixed;
179 }
180
181 const QFont& NetHackQtSettings::largeFont()
182 {
183     static int size[]={ 24, 18, 14, 12, 10 };
184     large.setPointSize(size[fontsize.currentIndex()]);
185     return large;
186 }
187
188 bool NetHackQtSettings::ynInMessages()
189 {
190     return !qt_compact_mode && !iflags.wc_popup_dialog;
191 }
192
193 NetHackQtSettings* qt_settings;
194
195 } // namespace nethack_qt4