| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2004-2005 Artur Wiebe * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | #include <QLayout> |
| 21 | #include <QDebug> |
| 22 | #include <QProgressDialog> |
| 23 | #include <QHeaderView> |
| 24 | #include <QInputDialog> |
| 25 | #include <QLineEdit> |
| 26 | |
| 27 | #include "history.h" |
| 28 | #include "common.h" |
| 29 | |
| 30 | |
| 31 | |
| 32 | #define COL_TAG_NR 0 // m_tags, this col is hidden. |
| 33 | #define COL_TAG_NAME 1 |
| 34 | #define COL_TAG_VAL 2 |
| 35 | |
| 36 | #define COL_MOVE_NR 0 // m_movelist |
| 37 | #define COL_MOVE 1 |
| 38 | #define COL_MOVE_COMM 2 |
| 39 | |
| 40 | |
| 41 | myHistory::myHistory(QWidget* parent) |
| 42 | : QFrame(parent) |
| 43 | { |
| 44 | //setFixedWidth(240); |
| 45 | |
| 46 | m_gamelist = new QComboBox(this); |
| 47 | connect(m_gamelist, SIGNAL(activated(int)), |
| 48 | this, SLOT(slot_game_selected(int))); |
| 49 | |
| 50 | m_taglist = new QTreeWidget(this); |
| 51 | m_taglist->setColumnCount(3); |
| 52 | m_taglist->header()->hide(); |
| 53 | m_taglist->setColumnHidden(COL_TAG_NR, true); |
| 54 | // m_taglist->header()->setStretchLastSection(true); |
| 55 | // m_taglist->header()->setResizeMode(QHeaderView::Stretch); |
| 56 | m_taglist->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 57 | connect(m_taglist, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), |
| 58 | this, SLOT(slot_modify_tag(QTreeWidgetItem*, int))); |
| 59 | |
| 60 | m_movelist = new QTreeWidget(this); |
| 61 | m_movelist->setColumnCount(3); |
| 62 | m_movelist->header()->setStretchLastSection(true); |
| 63 | //m_movelist->header()->setMovable(false); |
| 64 | m_movelist->setRootIsDecorated(false); |
| 65 | QStringList header; |
| 66 | header << "#" << tr("Move") << tr("Comment"); |
| 67 | m_movelist->setHeaderLabels(header); |
| 68 | // |
| 69 | connect(m_movelist, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), |
| 70 | this, SLOT(slot_modify_comment(QTreeWidgetItem*, int))); |
| 71 | connect(m_movelist, |
| 72 | SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), |
| 73 | this, |
| 74 | SLOT(slot_move(QTreeWidgetItem*, QTreeWidgetItem*))); |
| 75 | |
| 76 | // history |
| 77 | /* |
| 78 | gameUndo = new QAction(QIcon(":/icons/undo.svg"), tr("&Undo"), this); |
| 79 | connect(gameUndo, SIGNAL(triggered()), m_view, SLOT(slotUndo())); |
| 80 | |
| 81 | gameRedo = new QAction(QIcon(":/icons/redo.svg"), tr("&Redo"), this); |
| 82 | connect(gameRedo, SIGNAL(triggered()), m_view, SLOT(slotRedo())); |
| 83 | |
| 84 | gameContinue = new QAction(QIcon(":/icons/continue.svg"), |
| 85 | tr("&Continue"), this); |
| 86 | connect(gameContinue, SIGNAL(triggered()), m_view, SLOT(slotContinue())); |
| 87 | |
| 88 | |
| 89 | */ |
| 90 | m_mode_icon = new QLabel(this); |
| 91 | m_mode_icon->setFrameStyle(QFrame::Panel | QFrame::Sunken); |
| 92 | |
| 93 | m_undo = new QToolButton(this); |
| 94 | m_undo->setIcon(QIcon(":/icons/undo.svg")); |
| 95 | m_undo->setToolTip(tr("Undo")); |
| 96 | connect(m_undo, SIGNAL(clicked()), this, SLOT(slot_undo())); |
| 97 | |
| 98 | m_redo = new QToolButton(this); |
| 99 | m_redo->setIcon(QIcon(":/icons/redo.svg")); |
| 100 | m_redo->setToolTip(tr("Redo")); |
| 101 | connect(m_redo, SIGNAL(clicked()), this, SLOT(slot_redo())); |
| 102 | |
| 103 | m_cont = new QToolButton(this); |
| 104 | m_cont->setIcon(QIcon(":/icons/continue.svg")); |
| 105 | m_cont->setToolTip(tr("Continue")); |
| 106 | connect(m_cont, SIGNAL(clicked()), this, SLOT(slot_continue())); |
| 107 | |
| 108 | m_current = new QLabel(this); |
| 109 | |
| 110 | QHBoxLayout* history = new QHBoxLayout(); |
| 111 | history->addWidget(m_mode_icon); |
| 112 | //TODO history->addStretch(); |
| 113 | history->addWidget(m_undo); |
| 114 | history->addWidget(m_redo); |
| 115 | history->addWidget(m_cont); |
| 116 | history->addStretch(); |
| 117 | history->addWidget(m_current); |
| 118 | |
| 119 | // layout |
| 120 | QVBoxLayout* vb = new QVBoxLayout(this); |
| 121 | vb->setMargin(0); |
| 122 | vb->addWidget(m_gamelist, 0); |
| 123 | vb->addWidget(m_taglist, 2); |
| 124 | vb->addWidget(m_movelist, 4); |
| 125 | vb->addLayout(history); |
| 126 | |
| 127 | /* |
| 128 | * other stuff |
| 129 | */ |
| 130 | m_pdn = new Pdn(); |
| 131 | m_disable_moves = false; |
| 132 | // |
| 133 | m_paused = true; // little hack ensures a mode change. |
| 134 | m_freeplace = false; |
| 135 | set_mode(false); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | myHistory::~myHistory() |
| 140 | { |
| 141 | delete m_pdn; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void myHistory::clear() |
| 146 | { |
| 147 | m_gamelist->clear(); |
| 148 | m_pdn->clear(); |
| 149 | m_taglist->clear(); |
| 150 | m_movelist->clear(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void myHistory::setTag(PdnGame::Tag tag, const QString& val) |
| 155 | { |
| 156 | QTreeWidgetItem* item = 0; |
| 157 | QList<QTreeWidgetItem*> item_list = m_taglist->findItems( |
| 158 | tag_to_string(tag), Qt::MatchExactly, COL_TAG_NAME); |
| 159 | if(item_list.count()) { |
| 160 | if(item_list.count() == 1) |
| 161 | item = item_list[0]; |
| 162 | else |
| 163 | qDebug() << __PRETTY_FUNCTION__ << "ERR"; |
| 164 | } |
| 165 | |
| 166 | if(item) { |
| 167 | item->setText(COL_TAG_VAL, val); |
| 168 | } else { |
| 169 | item = new QTreeWidgetItem(m_taglist); |
| 170 | item->setText(COL_TAG_NR, QString::number(tag)); |
| 171 | item->setText(COL_TAG_NAME, tag_to_string(tag)); |
| 172 | item->setText(COL_TAG_VAL, val); |
| 173 | } |
| 174 | |
| 175 | if(tag==PdnGame::Type) { |
| 176 | item->setText(COL_TAG_VAL, val + " (" |
| 177 | + typeToString(QString("%1").arg(val).toInt()) |
| 178 | + ")"); |
| 179 | } |
| 180 | |
| 181 | m_game->set(tag, val); |
| 182 | //TODO m_taglist->resizeColumnToContents(COL_TAG_NAME); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | QString myHistory::getTag(PdnGame::Tag tag) |
| 187 | { |
| 188 | QList<QTreeWidgetItem*> item_list = m_taglist->findItems( |
| 189 | tag_to_string(tag), Qt::MatchExactly, COL_TAG_NAME); |
| 190 | if(item_list.count() == 1) |
| 191 | return item_list[0]->text(COL_TAG_VAL); |
| 192 | return ""; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | QString myHistory::tag_to_string(PdnGame::Tag tag) |
| 197 | { |
| 198 | switch(tag) { |
| 199 | case PdnGame::Date: return /*tr(*/"Date";//); |
| 200 | case PdnGame::Site: return /*tr(*/"Site";//); |
| 201 | case PdnGame::Type: return /*tr(*/"Type";//); |
| 202 | case PdnGame::Event: return /*tr(*/"Event";//); |
| 203 | case PdnGame::Round: return /*tr(*/"Round";//); |
| 204 | case PdnGame::White: return /*tr(*/"White";//); |
| 205 | case PdnGame::Black: return /*tr(*/"Black";//); |
| 206 | case PdnGame::Result: return /*tr(*/"Result";//); |
| 207 | } |
| 208 | |
| 209 | return "Site"; // FIXME |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void myHistory::appendMove(const QString& text, const QString& comm) |
| 214 | { |
| 215 | m_disable_moves = true; |
| 216 | |
| 217 | QTreeWidgetItem* new_item = new QTreeWidgetItem(m_movelist); |
| 218 | new_item->setText(COL_MOVE, text); |
| 219 | new_item->setText(COL_MOVE_COMM, comm); |
| 220 | |
| 221 | int move_nr = (m_movelist->topLevelItemCount() - 2) / 2; |
| 222 | PdnMove* m = m_game->getMove(move_nr); |
| 223 | |
| 224 | if(m_movelist->topLevelItemCount()%2) { |
| 225 | m->m_second = text; |
| 226 | m->m_comsecond = comm; |
| 227 | } else { |
| 228 | new_item->setText(COL_MOVE_NR, QString("%1.").arg(move_nr+1)); |
| 229 | m->m_first = text; |
| 230 | m->m_comfirst = comm; |
| 231 | } |
| 232 | |
| 233 | m_movelist->setCurrentItem(new_item); |
| 234 | m_movelist->scrollToItem(new_item); |
| 235 | |
| 236 | // TODO |
| 237 | m_movelist->resizeColumnToContents(COL_MOVE_NR); |
| 238 | |
| 239 | m_disable_moves = false; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void myHistory::slot_modify_comment(QTreeWidgetItem* item, int) |
| 244 | { |
| 245 | if(!item || item==m_movelist->topLevelItem(0) || m_paused) |
| 246 | return; |
| 247 | |
| 248 | bool ok; |
| 249 | QString new_text = QInputDialog::getText(this, tr("Set Comment"),//FIXME |
| 250 | tr("Comment")+":", QLineEdit::Normal, item->text(COL_MOVE_COMM), |
| 251 | &ok); |
| 252 | if(!ok) |
| 253 | return; |
| 254 | |
| 255 | new_text.remove('{'); |
| 256 | new_text.remove('}'); |
| 257 | if(new_text != item->text(COL_MOVE_COMM)) { |
| 258 | // gui |
| 259 | item->setText(COL_MOVE_COMM, new_text); |
| 260 | |
| 261 | // pdn |
| 262 | int index = m_movelist->indexOfTopLevelItem(item); |
| 263 | PdnMove* move = m_game->getMove((index - 1) / 2); |
| 264 | if(index%2==1) |
| 265 | move->m_comfirst = new_text; |
| 266 | else |
| 267 | move->m_comsecond = new_text; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void myHistory::slot_modify_tag(QTreeWidgetItem* item, int/* col*/) |
| 273 | { |
| 274 | if(!item || m_paused) |
| 275 | return; |
| 276 | |
| 277 | PdnGame::Tag tag =(PdnGame::Tag)item->text(COL_TAG_NR).toUInt(); |
| 278 | if(tag==PdnGame::Type) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | bool ok; |
| 283 | QString new_text = QInputDialog::getText(this, tr("Set Tag"),//FIXME |
| 284 | tr("Tag")+":", QLineEdit::Normal, item->text(COL_TAG_VAL), &ok); |
| 285 | if(!ok) |
| 286 | return; |
| 287 | |
| 288 | new_text.remove('"'); |
| 289 | new_text.remove('['); |
| 290 | new_text.remove(']'); |
| 291 | |
| 292 | if(new_text != item->text(COL_TAG_VAL)) { |
| 293 | item->setText(COL_TAG_VAL, new_text); |
| 294 | m_game->set(tag, new_text); |
| 295 | if(tag==PdnGame::Event) |
| 296 | m_gamelist->setItemText(m_gamelist->currentIndex(), |
| 297 | new_text); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | bool myHistory::openPdn(const QString& filename, QString& log_text) |
| 303 | { |
| 304 | if(!m_pdn->open(filename, this, tr("Reading file..."), log_text)) { |
| 305 | set_mode(false); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | set_mode(true); |
| 310 | |
| 311 | m_gamelist->clear(); |
| 312 | m_movelist->clear(); |
| 313 | m_taglist->clear(); |
| 314 | |
| 315 | QProgressDialog progress(this); |
| 316 | progress.setModal(true); |
| 317 | progress.setLabelText(tr("Importing games...")); |
| 318 | progress.setRange(0, m_pdn->count()); |
| 319 | progress.setMinimumDuration(0); |
| 320 | |
| 321 | for(int i=0; i<m_pdn->count(); ++i) { |
| 322 | if((i%10)==0) |
| 323 | progress.setValue(i); |
| 324 | m_gamelist->insertItem(i, m_pdn->game(i)->get(PdnGame::Event)); |
| 325 | } |
| 326 | |
| 327 | slot_game_selected(0); |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | bool myHistory::savePdn(const QString& fn) |
| 334 | { |
| 335 | return m_pdn->save(fn); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | void myHistory::slot_game_selected(int index) |
| 340 | { |
| 341 | if(index>=m_pdn->count()) { |
| 342 | qDebug() << __PRETTY_FUNCTION__ << "Index" << index |
| 343 | << "out of range >=" << m_pdn->count(); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | m_game = m_pdn->game(index); |
| 348 | m_movelist->clear(); |
| 349 | |
| 350 | QTreeWidgetItem* root = new QTreeWidgetItem(m_movelist); |
| 351 | for(int i=0; i<m_game->movesCount(); ++i) { |
| 352 | PdnMove* m = m_game->getMove(i); |
| 353 | |
| 354 | appendMove(m->m_first, m->m_comfirst); |
| 355 | if(m->m_second.length()) |
| 356 | appendMove(m->m_second, m->m_comsecond); |
| 357 | } |
| 358 | |
| 359 | setTag(PdnGame::Site, m_game->get(PdnGame::Site)); |
| 360 | setTag(PdnGame::Black, m_game->get(PdnGame::Black)); |
| 361 | setTag(PdnGame::White, m_game->get(PdnGame::White)); |
| 362 | setTag(PdnGame::Result, m_game->get(PdnGame::Result)); |
| 363 | setTag(PdnGame::Date, m_game->get(PdnGame::Date)); |
| 364 | setTag(PdnGame::Site, m_game->get(PdnGame::Site)); |
| 365 | setTag(PdnGame::Type, m_game->get(PdnGame::Type)); |
| 366 | setTag(PdnGame::Round, m_game->get(PdnGame::Round)); |
| 367 | setTag(PdnGame::Event, m_game->get(PdnGame::Event)); |
| 368 | |
| 369 | // signal to view |
| 370 | if(m_paused && !m_freeplace) { |
| 371 | emit previewGame(m_game->get(PdnGame::Type).toInt()); |
| 372 | } |
| 373 | |
| 374 | m_movelist->setCurrentItem(root); |
| 375 | slot_move(root, 0); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void myHistory::newPdn(const QString& event, bool freeplace) |
| 380 | { |
| 381 | m_freeplace = freeplace; |
| 382 | m_paused = !m_freeplace; // FIXME - needed to force view update. |
| 383 | set_mode(m_freeplace); |
| 384 | |
| 385 | PdnGame* game = m_pdn->newGame(); |
| 386 | game->set(PdnGame::Event, event); |
| 387 | |
| 388 | int index = m_gamelist->count(); |
| 389 | m_gamelist->insertItem(index, event); |
| 390 | m_gamelist->setCurrentIndex(index); |
| 391 | |
| 392 | slot_game_selected(index); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | QString myHistory::typeToString(int type) |
| 397 | { |
| 398 | switch(type) { |
| 399 | case ENGLISH: return tr("English draughts"); |
| 400 | case RUSSIAN: return tr("Russian draughts"); |
| 401 | }; |
| 402 | return tr("Unknown game type"); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void myHistory::set_mode(bool paused) |
| 407 | { |
| 408 | if(m_paused != paused) { |
| 409 | m_paused = paused; |
| 410 | |
| 411 | if(m_paused) { |
| 412 | if(m_freeplace) { |
| 413 | m_mode_icon->setPixmap(QPixmap(":/icons/freeplace.png")); |
| 414 | m_mode_icon->setToolTip(tr("Free Placement Mode")); |
| 415 | } else { |
| 416 | m_mode_icon->setPixmap(QPixmap(":/icons/paused.svg")); |
| 417 | m_mode_icon->setToolTip(tr("Paused Mode")); |
| 418 | } |
| 419 | } else { |
| 420 | m_mode_icon->setPixmap(QPixmap(":/icons/logo.svg")); |
| 421 | m_mode_icon->setToolTip(tr("Play Mode")); |
| 422 | } |
| 423 | |
| 424 | m_gamelist->setEnabled(m_paused); |
| 425 | //FIXME m_movelist->setEnabled(yes); |
| 426 | |
| 427 | emit newMode(m_paused, m_freeplace); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void myHistory::slot_move(QTreeWidgetItem* item, QTreeWidgetItem*) |
| 433 | { |
| 434 | // update history buttons. |
| 435 | bool curr_is_first = |
| 436 | (m_movelist->topLevelItem(0) == m_movelist->currentItem()); |
| 437 | bool curr_is_last = |
| 438 | (m_movelist->indexOfTopLevelItem(m_movelist->currentItem()) |
| 439 | == m_movelist->topLevelItemCount()-1); |
| 440 | |
| 441 | m_undo->setEnabled(!curr_is_first); |
| 442 | m_redo->setEnabled(!curr_is_last); |
| 443 | m_cont->setEnabled(m_paused); |
| 444 | |
| 445 | |
| 446 | // process |
| 447 | if(!item || !m_paused || m_disable_moves) |
| 448 | return; |
| 449 | |
| 450 | do_moves(); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void myHistory::history_undo(bool move_backwards) |
| 455 | { |
| 456 | int next = m_movelist->indexOfTopLevelItem(m_movelist->currentItem()) |
| 457 | + (move_backwards ? -1 : +1); |
| 458 | |
| 459 | if(next>=0 && next<m_movelist->topLevelItemCount()) |
| 460 | m_movelist->setCurrentItem(m_movelist->topLevelItem(next)); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void myHistory::do_moves() |
| 465 | { |
| 466 | QString moves; |
| 467 | QTreeWidgetItem* item = m_movelist->currentItem(); |
| 468 | for(int i=0; i<m_movelist->topLevelItemCount(); ++i) { |
| 469 | QTreeWidgetItem* cur = m_movelist->topLevelItem(i); |
| 470 | moves += cur->text(COL_MOVE) + MOVE_SPLIT; |
| 471 | if(m_movelist->topLevelItem(i)==item) |
| 472 | break; |
| 473 | } |
| 474 | emit applyMoves(moves); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | void myHistory::delete_moves() |
| 479 | { |
| 480 | int curr = m_movelist->indexOfTopLevelItem(m_movelist->currentItem()); |
| 481 | while(m_movelist->topLevelItemCount() > curr+1) { |
| 482 | delete m_movelist->topLevelItem( |
| 483 | m_movelist->topLevelItemCount()-1); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
| 488 | void myHistory::slot_undo() |
| 489 | { |
| 490 | set_mode(true); |
| 491 | history_undo(true); |
| 492 | do_moves(); |
| 493 | } |
| 494 | |
| 495 | void myHistory::slot_redo() |
| 496 | { |
| 497 | set_mode(true); |
| 498 | history_undo(false); |
| 499 | do_moves(); |
| 500 | } |
| 501 | |
| 502 | |
| 503 | void myHistory::slot_continue() |
| 504 | { |
| 505 | delete_moves(); |
| 506 | set_mode(false); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | void myHistory::slotWorking(bool b) |
| 511 | { |
| 512 | setEnabled(!b); |
| 513 | } |
| 514 |