Index: trunk/playground/games/nonogram/src/gui/grid.cpp =================================================================== --- trunk/playground/games/nonogram/src/gui/grid.cpp (revision 925586) +++ trunk/playground/games/nonogram/src/gui/grid.cpp (revision 925587) @@ -1,127 +1,129 @@ /** * grid.cpp * * Copyright 2008 Tom Vollerthun This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "grid.h" #include #include #include #include "grid_header.h" #include "grid_center.h" #include "colorselector.h" #include "ds/data.h" Grid::Grid(QWidget *parent) : QWidget(parent) { setPalette(QPalette(QColor(200, 200, 200))); setAutoFillBackground(true); colors = new ColorSelector(this); picturePreview = new OriginalSizePicture(this); central = new GridCenter(this); central->setVisible(true); column_header = new GridHeader(this, false); column_header->setVisible(true); line_header = new GridHeader(this, true); line_header->setVisible(true); int h_max = 5; int v_max = 6; QGridLayout *gridLayout = new QGridLayout; //upper left: preview and colorselection gridLayout->addWidget(picturePreview, 0, 0, 1, 1); gridLayout->addWidget(colors, 1, 0, 1, 1); //remainder: horizontal and vertical headings // and the actual grid. gridLayout->addWidget(line_header, 2, 0, h_max, 1); gridLayout->addWidget(column_header, 0, 1, 1, v_max); gridLayout->addWidget(central, 2, 1, h_max, v_max); gridLayout->setHorizontalSpacing(0); gridLayout->setVerticalSpacing(0); setLayout(gridLayout); } void Grid::setData(Data * data) { /*First try to disconnect from whichever data was connected before*/ Data* oldData = central->data(); if (oldData != NULL){ central->disconnect(); line_header->disconnect(); column_header->disconnect(); picturePreview->disconnect(); data->disconnect(); oldData->disconnect(); } kDebug() << "Setting Data"; line_header->setData(data); column_header->setData(data); central->setData(data); colors->setData(data); picturePreview->setData(data); //signals from the grid: tell the data to react to a click connect(central, SIGNAL(togglePosition(int, int)), data, SLOT(togglePosition(int, int))); connect(central, SIGNAL(toggle_UndefinedOn(int, int)), data, SLOT(toggle_UndefinedOn(int, int))); connect(central, SIGNAL(toggle_UndefinedOff(int, int)), data, SLOT(toggle_UndefinedOff(int, int))); //signal from data: make color-buttons adjust to available colors. connect(data, SIGNAL(colorsChanged(Data *)), colors, SLOT(setData(Data *))); //signal from data: repaint grid if data changes internally. connect(data, SIGNAL(positionChanged()), central, SLOT(update())); + connect(data, SIGNAL(positionChanged()), line_header, SLOT(update())); + connect(data, SIGNAL(positionChanged()), column_header, SLOT(update())); //repaint preview as well connect(data, SIGNAL(positionChanged()), picturePreview, SLOT(update())); /*if you add more signals, remember to add them to the remove list above, too*/ update(); line_header->update(); column_header->update(); central->update(); picturePreview->update(); kDebug() << "Finished setting Data"; } Data * Grid::data() { return central->data(); } Index: trunk/playground/games/nonogram/src/ds/data.cpp =================================================================== --- trunk/playground/games/nonogram/src/ds/data.cpp (revision 925586) +++ trunk/playground/games/nonogram/src/ds/data.cpp (revision 925587) @@ -1,455 +1,455 @@ /** * data.cpp * * Copyright 2008 Tom Vollerthun This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "data.h" #include #include Data::Data(int rows, int cols) { m_currentColor = Qt::black; m_backGround = Qt::white; rowList = new QList< QList * > (); colList = new QList< QList * > (); m_matrix = new QList< QList * > (); for (int x = 0; x * aList = new QList () ; rowList->append(aList); } for (int y = 0; y * aList = new QList () ; colList->append(aList); } for (int x = 0; x * aList = new QList () ; for (int y = 0; yappend(m); } m_matrix->append(aList); } } void Data::togglePosition(int x, int y) { MatrixEntry * m = entry(x, y); if (m->box_status == BOX_UNDEFINED || ( (m->box_color != m_currentColor) && (m->box_color != m_backGround) ) ) { setOn(m); } else if (m->box_status == BOX_ON) { setOff(m); } else { setUndefined(m); } emit positionChanged(x, y); emit positionChanged(); } void Data::toggle_UndefinedOn(int x, int y) { MatrixEntry * m = entry(x, y); if (m->box_status == BOX_ON && m->box_color == m_currentColor) { setUndefined(m); } else { setOn(m); } emit positionChanged(x, y); emit positionChanged(); } void Data::toggle_UndefinedOff(int x, int y) { MatrixEntry * m = entry(x, y); if (m->box_status == BOX_OFF) { setUndefined(m); } else { setOff(m); } emit positionChanged(x, y); emit positionChanged(); } void Data::setOn(MatrixEntry * m) { m->box_status = BOX_ON; m->box_color = m_currentColor; } void Data::setOff(MatrixEntry * m) { m->box_status = BOX_OFF; m->box_color = m_backGround; } void Data::setUndefined(MatrixEntry * m) { m->box_status = BOX_UNDEFINED; m->box_color = m_backGround; } void Data::resetGame() { //reset header-hints (active/inactive) - for (int i = 0; i < rowList->size(); ++i){ - for(int j = 0; i < rowList->at(i)->size(); j++){ + for (int x = 0; x < rowList->size(); ++x){ + for(int y = 0; y < rowList->at(x)->size(); y++){ - HintEntry * h = rowList->at(i)->at(j); + HintEntry * h = rowList->at(x)->at(y); h->active = true; } } - for (int i = 0; i < colList->size(); ++i){ - for(int j = 0; i < colList->at(i)->size(); j++){ + for (int x = 0; x < colList->size(); ++x){ + for(int y = 0; y < colList->at(x)->size(); y++){ - HintEntry * h = colList->at(i)->at(j); + HintEntry * h = colList->at(x)->at(y); h->active = true; } } //reset grid-boxes - for (int x = 0; x < m_matrix->size() ; ++x) { - for (int y = 0; y < m_matrix->at(x)->size(); ++y) { + for (int x = 0; x < getColumnCount() ; ++x) { + for (int y = 0; y < getRowCount() ; ++y) { - MatrixEntry * m = m_matrix->at(x)->at(y); + MatrixEntry * m = entry(x, y); setUndefined(m); } } kDebug() << "Game reset!"; emit positionChanged(); } void Data::setRowHints(int row, QList * values) { rowList->replace(row, values); } void Data::setRowHints(QList< QList *> * values) { rowList = values; } void Data::setColumnHints(int column, QList * values) { colList->replace(column, values); } void Data::setColumnHints(QList< QList *> * values) { colList = values; } QList * Data::availableColors() { return m_availableColors; } void Data::setAvailableColors(QList * colors) { m_availableColors = colors; kDebug() << "Sending colorsChanged signal"; emit colorsChanged(this); } void Data::update() { /*remove empty rows and columns at the beginning and the end*/ bool dropDone = false; for (int y = 0; y < rowList->size() && !dropDone; y++) { Data::HintEntry * hint = rowList->first()->first(); if (hint->amount == 0) { rowList->removeFirst(); } else { dropDone = true; } } for (int i = rowList->size() -1; i >=0; i--) { Data::HintEntry * hint = rowList->last()->first(); if (hint->amount == 0) { rowList->removeLast(); } else { dropDone = true; } } for (int x = 0; x < colList->size(); x++) { Data::HintEntry * hint = colList->first()->first(); if (hint->amount == 0) { colList->removeFirst(); } else { dropDone = true; } } for (int i = rowList->size() -1; i >=0; i--) { Data::HintEntry * hint = colList->last()->first(); if (hint->amount == 0) { colList->removeLast(); } else { dropDone = true; } } /*find all available colors*/ QList * availableColors = new QList(); for (int y = 0; y < rowList->size(); y++) { QList * row = rowList->at(y); for (int x = 0; x < row->size(); x++) { HintEntry * h = row->at(x); /*color of current pixel*/ QColor color = h->hintColor; if (color != Qt::white && !availableColors->contains(color)){ availableColors->append(color); } } } setAvailableColors(availableColors); m_matrix = new QList< QList * > (); /*fill the grid's content with undefined MatrixEntries.*/ for (int x = 0; xsize(); x++) { QList * aList = new QList () ; for (int y = 0; ysize(); y++) { MatrixEntry * m = new MatrixEntry(); setUndefined(m); aList->append(m); } m_matrix->append(aList); } kDebug() << "but matrix size is : [" << m_matrix->at(0)->size() << "/" << m_matrix->size() << "]"; } QList * Data::rowHints(int row) { QList * retval = rowList->at(row); return retval; } QList * Data::columnHints(int column) { QList * retval = colList->at(column); return retval; } int Data::getRowCount() { return rowList->size(); } int Data::getColumnCount() { return colList->size(); } QList< QList *> * Data::rowHints() { return rowList; } QList< QList *> * Data::columnHints() { return colList; } QList< QList *> * Data::matrix() { return m_matrix; } QString Data::toString(QList * entries) { QString entries_String = "|"; for (int i = 0; i < entries->size(); ++i) { HintEntry * entry = entries->at(i); if (entry->amount>0) { entries_String.append(QString::number(entry->amount) + ',' + entry->hintColor.name() + '|'); } } return entries_String; } QString Data::toString(QList< QList *> * entries) { QString entries_String = "-"; for (int i = 0; i < entries->size(); ++i) { QList * entry = entries->at(i); entries_String.append(toString(entry)); entries_String.append("-"); } return entries_String; } QString Data::toString(QList< QList *> * entries) { QString entries_String = "-"; for (int i = 0; i < entries->size(); ++i) { QList * aRow = entries->at(i); entries_String.append("|"); for (int j = 0; j < aRow->size(); ++j) { MatrixEntry * matrixEntry = aRow->at(j); entries_String.append(QString::number(matrixEntry->box_status) + ',' + matrixEntry->box_color.name() + '|'); } entries_String.append("-"); } return entries_String; } QList< QList *> * Data::fromString(const QString & hintString) { QList< QList *> *retval = new QList< QList *>(); QStringList hint_list_list = hintString.split("-", QString::SkipEmptyParts); for (int i = 0; i < hint_list_list.size(); ++i) { QString hint_list_string = hint_list_list.at(i); QList * aRow = new QList(); QStringList hint_list = hint_list_string.split("|", QString::SkipEmptyParts); if (hint_list.size() != 0) { //hintlist contains at least one hint for (int j = 0; j < hint_list.size(); ++j) { QString hint_string = hint_list.at(j); if (!hint_string.isEmpty() && !hint_string.isNull()) { QStringList hintEntryList = hint_string.split(",", QString::KeepEmptyParts); Data::HintEntry * hint = new Data::HintEntry(); hint->amount = hintEntryList.at(0).toInt(); hint->hintColor = QColor(hintEntryList.at(1)); hint->active = true; aRow->append(hint); } } } else { //hintlist doesn't contain any hint. Default to a "0" so there's at least something :) Data::HintEntry * hint = new Data::HintEntry(); hint->amount = 0; hint->hintColor = Qt::black; hint->active = true; aRow->append(hint); } retval->append(aRow); } return retval; } /** * Get the Data::MatrixEntry at position x/y * @param x the column (horizontal - left to right) * @param y the row (vertical - top to bottom) */ Data::MatrixEntry * Data::entry(int x, int y) const { if ( y >= m_matrix->size() || x >= m_matrix->at(0)->size() ) { kDebug() << "Accessing at [" << x << "/" << y << "]"; kDebug() << "but matrix size is only : [" << m_matrix->at(0)->size() << "/" << m_matrix->size() << "]"; kDebug() << "My name is " << m_description->name(); } MatrixEntry * m = m_matrix->at(y)->at(x); return m; } void Data::setDescription(Description * description) { m_description = description; } Description * Data::description() { return m_description; } void Data::setActiveColor(const QColor & c) { if (m_currentColor != c) { kDebug() << "Adjusting current color to " << c; m_currentColor = c; } }