per test
[rattatechess.git] / search_gui.cpp
blobd700dbb778955cb68fb05541d38dffc8c6d39d7c
1 /***************************************************************************
2 search_gui.cpp - Gui to view the search tree
3 -------------------
4 begin : Sat Oct 06 2007
5 copyright : (C) 2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "search_gui.h"
19 #include <stdarg.h>
20 #include <QWidget>
21 #include <QApplication>
22 #include <QTreeWidget>
23 #include <QHeaderView>
24 #include <QSocketNotifier>
25 #include <QTreeWidgetItem>
27 QString qPrintf(const char* fmt, ...) {
28 va_list ap;
30 va_start(ap, fmt);
31 int l = vsnprintf(NULL, 0, fmt, ap)+1;
32 va_end(ap);
34 char *str = (char*)alloca(l);
35 //char *str = (char*)malloc(l);
36 va_start(ap, fmt);
37 vsnprintf(str, l, fmt, ap);
38 va_end(ap);
40 QString retv(str);
42 //free(str);
43 return retv;
46 SearchGui::SearchGui(int& argc, char** argv)
48 printf("Hello!\n");
49 app = new QApplication(argc, argv);
50 tree_widget = new QTreeWidget();
51 tree_widget->header()->hide();
52 tree_widget->resize(640, 480);
53 tree_widget->show();
55 max_ply = 4;
57 process_events();
58 printf("Gui up!\n");
60 new QSocketNotifier( fileno(stdin), QSocketNotifier::Read, this);
61 new QSocketNotifier( fileno(stdin), QSocketNotifier::Exception, this);
64 SearchGui::~SearchGui()
66 delete tree_widget;
67 delete app;
70 void SearchGui::wait_input()
72 while(1)
74 if(input_available())
75 return;
76 app->processEvents(QEventLoop::WaitForMoreEvents);
80 void SearchGui::process_events()
82 while(app->hasPendingEvents() && !input_available())
83 app->processEvents();
86 void SearchGui::apply_flags(QTreeWidgetItem* w, int flags)
88 QFont font = w->font(0);
89 if(flags & Italic)
90 font.setItalic(true);
91 if(flags & Bold)
92 font.setBold(true);
93 if(flags & NoItalic)
94 font.setItalic(false);
95 if(flags & NoBold)
96 font.setBold(false);
97 w->setFont(0, font);
98 if(flags & Red)
99 w->setForeground(0, Qt::red);
100 if(flags & Gray)
101 w->setForeground(0, Qt::gray);
102 if(flags & Green)
103 w->setForeground(0, Qt::green);
104 if(flags & Blue)
105 w->setForeground(0, Qt::blue);
106 if(flags & Magenta)
107 w->setForeground(0, Qt::magenta);
110 void SearchGui::init_root()
112 tree_widget->clear();
115 void SearchGui::new_root_level(int depth)
117 QTreeWidgetItem *item = new QTreeWidgetItem(QStringList()<<qPrintf("ROOT(%d)", depth));
118 tree_widget->addTopLevelItem(item);
119 item->setExpanded(true);
122 void SearchGui::notify_value(int ply, int value, int nodecount, int newflags)
124 if(ply>max_ply)
125 return;
127 QTreeWidgetItem *item = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
128 for(int i=0;i<=ply;i++)
129 item = (item && item->childCount()) ? item->child(item->childCount()-1) : NULL;
130 if(!item)
131 return;
132 item->setText(0, qPrintf( qPrintable(item->text(0)), value, nodecount ) );
133 apply_flags(item, newflags);
136 void SearchGui::notify_msg(int ply, const char* str, int flags)
138 if(ply>max_ply)
139 return;
141 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
142 for(int i=0;i<ply;i++)
143 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
144 if(!parent)
145 return;
146 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<< QString(str) );
147 if(parent->isExpanded())
148 tree_widget->scrollToItem(item);
149 apply_flags(item, flags);
152 void SearchGui::notify(const Board *board, Move m, int ply, int depth, int heuval, int alpha, int beta, int flags)
154 if(ply>max_ply)
155 return;
157 char str[64];
158 if(m == Move::None())
159 strcpy(str, "[NULL]");
160 else
162 SaveBuf tmp;
163 Board b = *board;
164 b.undo_move(m, tmp);
165 b.move_to_alg(str, m);
166 b.do_move(m, tmp);
167 if(heuval != -INF)
168 sprintf(str+strlen(str), "(%d)", heuval);
170 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
171 for(int i=0;i<ply;i++)
172 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
173 if(!parent)
174 return;
175 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
176 qPrintf("%s -> %%d @%d [%d .. %d] nc=%%d", str, depth, alpha, beta) );
177 if(parent->isExpanded())
178 tree_widget->scrollToItem(item);
179 apply_flags(item, flags);
182 void SearchGui::notify_eval(int ply, int value, int alpha, int beta, int flags)
184 if(ply>max_ply)
185 return;
187 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
188 for(int i=0;i<ply;i++)
189 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
190 if(!parent)
191 return;
192 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
193 qPrintf("[EVAL] -> %d [%d .. %d]", value, alpha, beta) );
194 if(parent->isExpanded())
195 tree_widget->scrollToItem(item);
196 apply_flags(item, flags);
199 void SearchGui::notify_hash(int ply, int lower, int upper, int depth, int alpha, int beta, Move best, bool write, int flags)
201 if(ply>max_ply)
202 return;
204 char buf[32];
205 if(best == Move::None())
206 strcpy(buf, "none");
207 else
208 Board::move_to_coord(buf, best);
209 QTreeWidgetItem *parent = tree_widget->topLevelItem(tree_widget->topLevelItemCount()-1);
210 for(int i=0;i<ply;i++)
211 parent = (parent && parent->childCount()) ? parent->child(parent->childCount()-1) : NULL;
212 if(!parent)
213 return;
214 QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList()<<
215 qPrintf("[%s] -> (%d .. %d) <%s> [%d .. %d]", write?"HTWR":"HTLK", lower, upper, buf, alpha, beta) );
216 if(parent->isExpanded())
217 tree_widget->scrollToItem(item);
218 apply_flags(item, flags);
221 #include "search_gui.moc"