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);
60 new QSocketNotifier( fileno(stdin
), QSocketNotifier::Read
, this);
61 new QSocketNotifier( fileno(stdin
), QSocketNotifier::Exception
, this);
64 SearchGui::~SearchGui()
70 void SearchGui::wait_input()
76 app
->processEvents(QEventLoop::WaitForMoreEvents
);
80 void SearchGui::process_events()
82 while(app
->hasPendingEvents() && !input_available())
86 void SearchGui::apply_flags(QTreeWidgetItem
* w
, int flags
)
88 QFont font
= w
->font(0);
94 font
.setItalic(false);
99 w
->setForeground(0, Qt::red
);
101 w
->setForeground(0, Qt::gray
);
103 w
->setForeground(0, Qt::green
);
105 w
->setForeground(0, Qt::blue
);
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
)
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
;
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
)
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
;
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
)
158 if(m
== Move::None())
159 strcpy(str
, "[NULL]");
165 b
.move_to_alg(str
, m
);
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
;
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
)
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
;
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
)
205 if(best
== Move::None())
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
;
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"