| 1 | #include "sgfpreview.h" |
| 2 | #include "ui_sgfpreview.h" |
| 3 | #include "displayboard.h" |
| 4 | #include "sgfparser.h" |
| 5 | #include "gamedata.h" |
| 6 | #include "defines.h" |
| 7 | #include "mainwindow.h" |
| 8 | #include "boardwindow.h" |
| 9 | |
| 10 | SGFPreview::SGFPreview(QWidget *parent) : |
| 11 | QWidget(parent), |
| 12 | ui(new Ui::SGFPreview) |
| 13 | { |
| 14 | MW_SGFparser = new SGFParser(NULL); |
| 15 | GameLoaded = NULL; |
| 16 | ui->setupUi(this); |
| 17 | } |
| 18 | |
| 19 | SGFPreview::~SGFPreview() |
| 20 | { |
| 21 | if (GameLoaded != NULL) |
| 22 | { |
| 23 | delete GameLoaded; |
| 24 | GameLoaded = NULL; |
| 25 | } |
| 26 | delete ui; |
| 27 | delete MW_SGFparser; |
| 28 | } |
| 29 | |
| 30 | void SGFPreview::clearData() |
| 31 | { |
| 32 | if (GameLoaded != NULL) |
| 33 | { |
| 34 | delete GameLoaded; |
| 35 | GameLoaded = NULL; |
| 36 | } |
| 37 | ui->displayBoard->clearData(); |
| 38 | |
| 39 | ui->File_WhitePlayer->setText(""); |
| 40 | ui->File_BlackPlayer->setText(""); |
| 41 | ui->File_Date->setText(""); |
| 42 | ui->File_Handicap->setText(""); |
| 43 | ui->File_Result->setText(""); |
| 44 | ui->File_Komi->setText(""); |
| 45 | ui->File_Size->setText(""); |
| 46 | } |
| 47 | |
| 48 | void SGFPreview::setPath(QString path) |
| 49 | { |
| 50 | //if (QFileInfo("path").isFile() == false) |
| 51 | // return; |
| 52 | clearData(); |
| 53 | |
| 54 | //fileLoaded = path.toLatin1().constData(); |
| 55 | SGFloaded = MW_SGFparser->loadFile(path); |
| 56 | if (SGFloaded == NULL) |
| 57 | { |
| 58 | emit isValidSGF(false); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | GameLoaded = MW_SGFparser->initGame(SGFloaded, path); |
| 63 | if (GameLoaded == NULL) |
| 64 | { |
| 65 | emit isValidSGF(false); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | GameLoaded->gameMode = modeEdit; |
| 70 | |
| 71 | QString komi, hcp, sz; |
| 72 | komi.setNum(GameLoaded->komi); |
| 73 | hcp.setNum(GameLoaded->handicap); |
| 74 | sz.setNum(GameLoaded->board_size); |
| 75 | |
| 76 | ui->File_WhitePlayer->setText(GameLoaded->white_name); |
| 77 | ui->File_BlackPlayer->setText(GameLoaded->black_name); |
| 78 | ui->File_Date->setText(GameLoaded->date); |
| 79 | ui->File_Handicap->setText(hcp); |
| 80 | ui->File_Result->setText(GameLoaded->result); |
| 81 | ui->File_Komi->setText(komi); |
| 82 | ui->File_Size->setText(sz); |
| 83 | |
| 84 | DisplayBoard* board = ui->displayBoard; |
| 85 | board->clearData(); |
| 86 | if (board->getSize() != GameLoaded->board_size) |
| 87 | board->init(GameLoaded->board_size); |
| 88 | |
| 89 | board->displayHandicap(GameLoaded->handicap); |
| 90 | |
| 91 | QString s = SGFloaded.trimmed(); |
| 92 | int end_main = s.indexOf(")("); |
| 93 | if (end_main == -1) |
| 94 | end_main = s.size(); |
| 95 | int a_offset = QChar::fromLatin1('a').unicode() - 1 ; |
| 96 | int cursor = 0; |
| 97 | int x,y; |
| 98 | int nb_displayed = 20; |
| 99 | QString coords; |
| 100 | |
| 101 | cursor = s.indexOf(";B["); |
| 102 | |
| 103 | while ((cursor >0) && (cursor < end_main) && (nb_displayed--) ) |
| 104 | { |
| 105 | x = s.at(cursor+3).unicode() - a_offset; |
| 106 | y = s.at(cursor+4).unicode() - a_offset; |
| 107 | board->updateStone(stoneBlack,x,y); |
| 108 | cursor = s.indexOf(";B[",cursor +1); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | cursor = s.indexOf(";W["); |
| 113 | nb_displayed = 20; |
| 114 | |
| 115 | while ( (cursor >0) && (cursor < end_main) && (nb_displayed--) ) |
| 116 | { |
| 117 | x = s.at(cursor+3).unicode() - a_offset; |
| 118 | y = s.at(cursor+4).unicode() - a_offset; |
| 119 | board->updateStone(stoneWhite,x,y); |
| 120 | cursor = s.indexOf(";W[",cursor +1); |
| 121 | |
| 122 | } |
| 123 | emit isValidSGF(true); |
| 124 | } |
| 125 | |
| 126 | // Maybe handle computer games separately. |
| 127 | // Here is the old code that was supposed to do that. |
| 128 | /*void MainWindow::slot_loadComputerFile(const QModelIndex & topLeft, const QModelIndex & bottomRight ) |
| 129 | { |
| 130 | QVariant v = topLeft.data(QDirModel::FilePathRole); |
| 131 | |
| 132 | if (!selectFile(topLeft)) |
| 133 | { |
| 134 | ui.button_loadComputerGame->setDisabled(true); |
| 135 | } else { |
| 136 | GameLoaded->gameMode = modeComputer; |
| 137 | ui.button_loadComputerGame->setEnabled(true); |
| 138 | |
| 139 | QString komi, hcp, sz; |
| 140 | komi.setNum(GameLoaded->komi); |
| 141 | hcp.setNum(GameLoaded->handicap); |
| 142 | sz.setNum(GameLoaded->board_size); |
| 143 | |
| 144 | ui.File_WhitePlayer->setText(GameLoaded->white_name); |
| 145 | ui.File_BlackPlayer->setText(GameLoaded->black_name); |
| 146 | ui.File_Date->setText(GameLoaded->date); |
| 147 | ui.File_Handicap->setText(hcp); |
| 148 | ui.File_Result->setText(GameLoaded->result); |
| 149 | ui.File_Komi->setText(komi); |
| 150 | ui.File_Size->setText(sz); |
| 151 | |
| 152 | displayGame(ui.displayBoard); |
| 153 | } |
| 154 | }*/ |