1 /***************************************************************************
2 search_gui.cpp - Gui to view the search tree
4 begin : Sat Oct 06 2007
5 copyright : (C) 2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "search_gui.h"
21 #include <QApplication>
22 #include <QTreeWidget>
23 #include <QHeaderView>
24 #include <QSocketNotifier>
25 #include <QTreeWidgetItem>
27 QString
qPrintf(const char* fmt
, ...) {
31 int l
= vsnprintf(NULL
, 0, fmt
, ap
)+1;
34 char *str
= (char*)alloca(l
);
35 //char *str = (char*)malloc(l);
37 vsnprintf(str
, l
, fmt
, ap
);
46 SearchGui::SearchGui(int& argc
, char** argv
)
49 app
= new QApplication(argc
, argv
);
50 tree_widget
= new QTreeWidget();
51 tree_widget
->header()->hide();
52 tree_widget
->resize(640, 480);
57 new QSocketNotifier( fileno(stdin
), QSocketNotifier::Read
, this);
58 new QSocketNotifier( fileno(stdin
), QSocketNotifier::Exception
, this);
61 SearchGui::~SearchGui()
67 void SearchGui::wait_input()
73 app
->processEvents(QEventLoop::WaitForMoreEvents
);
77 void SearchGui::process_events()
79 while(app
->hasPendingEvents())
83 void SearchGui::apply_flags(QTreeWidgetItem
* w
, int flags
)
85 QFont font
= w
->font(0);
91 font
.setItalic(false);
96 w
->setForeground(0, Qt::red
);
98 w
->setForeground(0, Qt::gray
);
100 w
->setForeground(0, Qt::green
);
102 w
->setForeground(0, Qt::blue
);
104 w
->setForeground(0, Qt::magenta
);
107 void SearchGui::init_root()
109 tree_widget
->clear();
112 void SearchGui::new_root_level(int depth
)
114 QTreeWidgetItem
*item
= new QTreeWidgetItem(QStringList()<<qPrintf("ROOT(%d)", depth
));
115 tree_widget
->addTopLevelItem(item
);
116 item
->setExpanded(true);
119 void SearchGui::notify_value(int ply
, int value
, int nodecount
, int newflags
)
124 QTreeWidgetItem
*item
= tree_widget
->topLevelItem(tree_widget
->topLevelItemCount()-1);
125 for(int i
=0;i
<=ply
;i
++)
126 item
= (item
&& item
->childCount()) ? item
->child(item
->childCount()-1) : NULL
;
129 item
->setText(0, qPrintf( qPrintable(item
->text(0)), value
, nodecount
) );
130 apply_flags(item
, newflags
);
133 void SearchGui::notify(Move m
, int ply
, int depth
, int heuval
, int alpha
, int beta
, int flags
)
139 if(m
== Move::None())
140 strcpy(str
, "[NULL]");
143 Engine::eng()->board
.undo_move(m
);
144 Engine::eng()->move_to_alg(str
, &m
);
145 Engine::eng()->board
.do_move(m
);
147 sprintf(str
+strlen(str
), "(%d)", heuval
);
149 QTreeWidgetItem
*parent
= tree_widget
->topLevelItem(tree_widget
->topLevelItemCount()-1);
150 for(int i
=0;i
<ply
;i
++)
151 parent
= (parent
&& parent
->childCount()) ? parent
->child(parent
->childCount()-1) : NULL
;
154 QTreeWidgetItem
*item
= new QTreeWidgetItem(parent
, QStringList()<<
155 qPrintf("%s -> %%d @%d [%d .. %d] nc=%%d", str
, depth
, alpha
, beta
) );
156 if(parent
->isExpanded())
157 tree_widget
->scrollToItem(item
);
158 apply_flags(item
, flags
);
161 void SearchGui::notify_eval(int ply
, int value
, int alpha
, int beta
, int flags
)
166 QTreeWidgetItem
*parent
= tree_widget
->topLevelItem(tree_widget
->topLevelItemCount()-1);
167 for(int i
=0;i
<ply
;i
++)
168 parent
= (parent
&& parent
->childCount()) ? parent
->child(parent
->childCount()-1) : NULL
;
171 QTreeWidgetItem
*item
= new QTreeWidgetItem(parent
, QStringList()<<
172 qPrintf("[EVAL] -> %d [%d .. %d]", value
, alpha
, beta
) );
173 if(parent
->isExpanded())
174 tree_widget
->scrollToItem(item
);
175 apply_flags(item
, flags
);
178 void SearchGui::notify_hash(int ply
, int lower
, int upper
, int depth
, int alpha
, int beta
, Move best
, bool write
, int flags
)
184 if(best
== Move::None())
187 Engine::eng()->move_to_coord(buf
, &best
);
188 QTreeWidgetItem
*parent
= tree_widget
->topLevelItem(tree_widget
->topLevelItemCount()-1);
189 for(int i
=0;i
<ply
;i
++)
190 parent
= (parent
&& parent
->childCount()) ? parent
->child(parent
->childCount()-1) : NULL
;
193 QTreeWidgetItem
*item
= new QTreeWidgetItem(parent
, QStringList()<<
194 qPrintf("[%s] -> (%d .. %d) <%s> [%d .. %d]", write
?"HTWR":"HTLK", lower
, upper
, buf
, alpha
, beta
) );
195 if(parent
->isExpanded())
196 tree_widget
->scrollToItem(item
);
197 apply_flags(item
, flags
);
200 #include "search_gui.moc"