File: moveTree.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (121 lines) | stat: -rw-r--r-- 2,761 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "moveTree.h"
#include "viewer.h"
#include "osl/eval/pieceEval.h"

#if QT_VERSION >= 0x050000
#  include <QtWidgets>
#else
#  include <QtGui>
#endif

MoveTree::MoveTree(QWidget *parent)
  : QTreeWidget(parent)
{
  setRootIsDecorated(true);
}

MoveTree::~MoveTree()
{
}

void MoveTree::buildContextMenu(QMenu *menu)
{
  QAction *expandBestAction = new QAction(tr("Expand Best"), this);
  connect(expandBestAction, SIGNAL(triggered()),
	  this, SLOT(expandBest()));
  menu->addAction(expandBestAction);
  QAction *closeAllAction = new QAction(tr("Close All"), this);
  connect(closeAllAction, SIGNAL(triggered()),
	  this, SLOT(closeAll()));
  menu->addAction(closeAllAction);

  menu->addSeparator();
  QAction *moveGenerateAction = new QAction(tr("Show Generated &Moves"), this);
  connect(moveGenerateAction, SIGNAL(triggered()),
	  this, SIGNAL(moveGenerate()));
  menu->addAction(moveGenerateAction);
}

void MoveTree::contextMenuEvent(QContextMenuEvent *event)
{
  QMenu menu(this);
  buildContextMenu(&menu);
  menu.exec(event->globalPos());
}

void MoveTree::closeAll()
{
  QTreeWidgetItemIterator it(this);
  while (*it)
  {
    (*it)->setExpanded(false);
    ++it;
  }
}

void MoveTree::expandBest()
{
  QList<QTreeWidgetItem *> selected = selectedItems();
  if (selected.empty())
    return;

  MoveTreeItem *item = (MoveTreeItem *)selected[0];
  expandBestChildren(item);
}

void MoveTree::expandBestChildren(MoveTreeItem *item)
{
  if (!item)
    return;

  item->setExpanded(true);
  for (int i=0; i<item->childCount(); ++i)
  {
    MoveTreeItem *child = (MoveTreeItem *)item->child(i);
    if (child->isBestMove())
    {
      child->setExpanded(true);
      expandBestChildren(child);
      break;
    }
  }
}

bool MoveTreeItem::operator<(const QTreeWidgetItem& other) const
{
  const int col = treeWidget()->sortColumn();
  if (col != 0)
  {
    return text(col) < other.text(col);
  }

  const MoveTreeItem& i = (const MoveTreeItem&)other;
  const osl::Move m2 = i.getMove();

  if ((! move.isNormal()) && (! m2.isNormal()))
    return true;
  else if (! move.isNormal())
    return false;
  else if (! m2.isNormal())
    return true;

  if (move.to().x() < m2.to().x())
    return true;
  else if (move.to().x() > m2.to().x())
    return false;
  else
  {
    if (move.to().y() < m2.to().y())
      return true;
    else if (move.to().y() > m2.to().y())
      return false;
    else
    {
      const int x1 = osl::eval::Ptype_Eval_Table.value(move.ptype()) +
                     osl::eval::Ptype_Eval_Table.value(osl::unpromote(move.ptype()));
      const int x2 = osl::eval::Ptype_Eval_Table.value(m2.ptype()) +
                     osl::eval::Ptype_Eval_Table.value(osl::unpromote(m2.ptype()));
      return (x1 < x2);
    }
  }
}