OSDN Git Service

upgrade to 3.6.1
[jnethack/source.git] / win / Qt4 / qt4msg.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 // qt4msg.cpp -- a message window
6
7 extern "C" {
8 #include "hack.h"
9 }
10 #undef Invisible
11 #undef Warning
12 #undef index
13 #undef msleep
14 #undef rindex
15 #undef wizard
16 #undef yn
17 #undef min
18 #undef max
19
20 #include <QtGui/QtGui>
21 #if QT_VERSION >= 0x050000
22 #include <QtWidgets/QtWidgets>
23 #endif
24 #include "qt4msg.h"
25 #include "qt4msg.moc"
26 #include "qt4map.h"
27 #include "qt4set.h"
28 #include "qt4str.h"
29
30 namespace nethack_qt4 {
31
32 NetHackQtMessageWindow::NetHackQtMessageWindow() :
33     list(new QListWidget())
34 {
35     list->setFocusPolicy(Qt::NoFocus);
36     ::iflags.window_inited = 1;
37     map = 0;
38     currgetmsg = 0;
39     connect(qt_settings,SIGNAL(fontChanged()),this,SLOT(updateFont()));
40     updateFont();
41 }
42
43 NetHackQtMessageWindow::~NetHackQtMessageWindow()
44 {
45     ::iflags.window_inited = 0;
46     delete list;
47 }
48
49 QWidget* NetHackQtMessageWindow::Widget() { return list; }
50
51 void NetHackQtMessageWindow::setMap(NetHackQtMapWindow2* m)
52 {
53     map = m;
54     updateFont();
55 }
56
57 void NetHackQtMessageWindow::updateFont()
58 {
59     list->setFont(qt_settings->normalFont());
60     if ( map )
61         map->setFont(qt_settings->normalFont());
62 }
63
64 void NetHackQtMessageWindow::Scroll(int dx, int dy)
65 {
66     //RLC Is this necessary?
67     //RLC list->Scroll(dx,dy);
68 }
69
70 void NetHackQtMessageWindow::Clear()
71 {
72     if ( map )
73         map->clearMessages();
74 }
75
76 void NetHackQtMessageWindow::ClearMessages()
77 {
78     if (list)
79         list->clear();
80 }
81
82 void NetHackQtMessageWindow::Display(bool block)
83 {
84     if (changed) {
85         list->repaint();
86         changed=false;
87     }
88 }
89
90 const char * NetHackQtMessageWindow::GetStr(bool init)
91 {
92     if (init)
93         currgetmsg = 0;
94
95     QListWidgetItem *item = list->item(currgetmsg++);
96     if (item) {
97         QString str = item->text();
98         //raw_printf("getstr[%i]='%s'", currgetmsg, str.toLatin1().constData());
99         return str.toLatin1().constData();
100     }
101     return NULL;
102 }
103
104 void NetHackQtMessageWindow::PutStr(int attr, const QString& text)
105 {
106 #ifdef USER_SOUNDS
107     play_sound_for_message(text.toLatin1().constData());
108 #endif
109
110     changed=true;
111
112     // If the line is output from the "/" command, map the first character
113     // as a symbol
114     QString text2;
115     if (text.mid(1, 3) == "   ") {
116         text2 = QChar(cp437(text.at(0).unicode())) + text.mid(1);
117     } else {
118         text2 = text;
119     }
120 #if 0
121     QListWidgetItem *item = new QListWidgetItem(text2);
122
123     QFont font = item->font();
124     font.setUnderline(attr == ATR_ULINE);
125     font.setWeight((attr == ATR_BOLD) ? QFont::Bold : QFont::Normal);
126     item->setFont(font);
127
128     QColor fg = item->foreground().color();
129     QColor bg = item->background().color();
130     if (attr == ATR_DIM)
131     {
132         fg.setAlpha(fg.alpha() / 2);
133     }
134     if (attr == ATR_INVERSE)
135     {
136         QColor swap;
137         swap = fg; fg = bg; bg = swap;
138     }
139     item->setForeground(fg);
140     item->setBackground(bg);
141 #endif
142
143     // ATR_BLINK not supported
144     if (list->count() >= ::iflags.msg_history)
145         delete list->item(0);
146     list->addItem(text2);
147
148     // Force scrollbar to bottom
149     list->setCurrentRow(list->count()-1);
150
151     if ( map )
152         map->putMessage(attr, text2);
153 }
154
155 } // namespace nethack_qt4