| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2002-2003 Andi Peredri * |
| 3 | * [email protected] * |
| 4 | * Copyright (C) 2004-2005 Artur Wiebe |
| 5 | * [email protected] |
| 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program; if not, write to the * |
| 19 | * Free Software Foundation, Inc., * |
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 21 | ***************************************************************************/ |
| 22 | |
| 23 | #include <QPainter> |
| 24 | #include <QMouseEvent> |
| 25 | #include <QDebug> |
| 26 | |
| 27 | #include "theme.h" |
| 28 | #include "field.h" |
| 29 | #include "common.h" |
| 30 | #include "checkers.h" |
| 31 | |
| 32 | |
| 33 | Field::Field(QObject* parent,int i) |
| 34 | : QObject(parent) |
| 35 | { |
| 36 | pixmap = new QPixmap(128, 128); |
| 37 | pixmap_valid = false; |
| 38 | |
| 39 | m_number=i; |
| 40 | |
| 41 | show_frame = false; |
| 42 | |
| 43 | m_show_label = true; |
| 44 | |
| 45 | setup_mode = false; |
| 46 | |
| 47 | m_pattern_id = 0; |
| 48 | m_checker_id = FREE; |
| 49 | } |
| 50 | |
| 51 | Field::~Field() { |
| 52 | if (pixmap) { |
| 53 | delete pixmap; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void Field::beginSetup() { |
| 58 | Q_ASSERT(! setup_mode); |
| 59 | //qInfo("beginSetup(%d)", m_number); |
| 60 | setup_mode = true; |
| 61 | } |
| 62 | |
| 63 | void Field::endSetup() { |
| 64 | Q_ASSERT(setup_mode); |
| 65 | //qInfo("endSetup(%d)", m_number); |
| 66 | |
| 67 | setup_mode = false; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | void Field::paintEvent(QPaintEvent*) |
| 73 | { |
| 74 | QPainter p(this); |
| 75 | |
| 76 | draw(); |
| 77 | Q_ASSERT(pixmap_valid); |
| 78 | p.drawPixmap(0, 0, *pixmap); |
| 79 | |
| 80 | p.end(); |
| 81 | } |
| 82 | */ |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | void Field::mousePressEvent(QMouseEvent* me) |
| 87 | { |
| 88 | if(me->button() != Qt::LeftButton) |
| 89 | return; |
| 90 | emit click(m_number); |
| 91 | } |
| 92 | */ |
| 93 | |
| 94 | void Field::invalidate() { |
| 95 | pixmap_valid = false; |
| 96 | } |
| 97 | |
| 98 | void Field::draw() |
| 99 | { |
| 100 | if (pixmap_valid) { |
| 101 | return; |
| 102 | } |
| 103 | /*if (pixmap) { |
| 104 | delete pixmap; |
| 105 | }*/ |
| 106 | /*QSize mySize = size(); |
| 107 | if (mySize.height() == 0 || mySize.width() == 0) { |
| 108 | return; |
| 109 | }*/ |
| 110 | //qInfo("Draw: %d x %d", mySize.width(), mySize.height()); |
| 111 | //pixmap = new QPixmap(128, 128); |
| 112 | |
| 113 | pixmap->fill(Qt::white); |
| 114 | QPainter paint; |
| 115 | paint.begin(pixmap); |
| 116 | //paint.setFont(font()); |
| 117 | |
| 118 | if (m_pattern_id) { |
| 119 | QPixmap& pattern = m_theme->getPattern(m_pattern_id); |
| 120 | if (! pattern.isNull()) { |
| 121 | paint.drawPixmap(0, 0, pattern); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // notation |
| 126 | paint.setPen(Qt::white); |
| 127 | QRect not_rect = paint.boundingRect(2, 2, 0, 0, Qt::AlignLeft, m_label); |
| 128 | QPixmap& checker = m_theme->getChecker(m_checker_id, m_bottom_is_white); |
| 129 | if (m_show_above) { |
| 130 | if (! checker.isNull()) |
| 131 | paint.drawPixmap(0, 0, checker); |
| 132 | if (m_show_label) { |
| 133 | paint.fillRect(not_rect, Qt::black); |
| 134 | paint.drawText(not_rect, Qt::AlignTop|Qt::AlignLeft, m_label); |
| 135 | } |
| 136 | } else { |
| 137 | if (m_show_label) |
| 138 | paint.drawText(not_rect, Qt::AlignTop|Qt::AlignLeft, m_label); |
| 139 | if (! checker.isNull()) |
| 140 | paint.drawPixmap(0, 0, checker); |
| 141 | } |
| 142 | |
| 143 | if (show_frame) { |
| 144 | QPixmap& frame = m_theme->getFrame(); |
| 145 | paint.drawPixmap(0, 0, frame); |
| 146 | } |
| 147 | |
| 148 | paint.end(); |
| 149 | //update(); |
| 150 | pixmap_valid = true; |
| 151 | } |
| 152 | |
| 153 | void Field::draw(QPainter& painter, QRect rect) { |
| 154 | m_rect = rect; |
| 155 | draw(); |
| 156 | Q_ASSERT(pixmap_valid); |
| 157 | painter.drawPixmap(rect.x(), rect.y(), *pixmap); |
| 158 | } |
| 159 | |
| 160 | const QRect& Field::rect() const { |
| 161 | return m_rect; |
| 162 | } |
| 163 | |
| 164 | void Field::showFrame(bool b) |
| 165 | { |
| 166 | if(show_frame != b) { |
| 167 | show_frame = b; |
| 168 | invalidate(); |
| 169 | draw(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void Field::setPattern(int i) |
| 174 | { |
| 175 | if (i != m_pattern_id) { |
| 176 | m_pattern_id = i; |
| 177 | invalidate(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void Field::setTheme(Theme* theme) { |
| 182 | Q_ASSERT(theme); |
| 183 | m_theme = theme; |
| 184 | invalidate(); |
| 185 | draw(); |
| 186 | } |
| 187 | |
| 188 | void Field::set(int item, bool bottom_is_white) { |
| 189 | if (m_checker_id != item || m_bottom_is_white != bottom_is_white) { |
| 190 | m_checker_id = item; |
| 191 | m_bottom_is_white = bottom_is_white; |
| 192 | invalidate(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void Field::setLabel(const QString& str) |
| 197 | { |
| 198 | if(m_label!=str) { |
| 199 | m_label=str; |
| 200 | invalidate(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void Field::showLabel(bool s, bool a) |
| 206 | { |
| 207 | if(s!=m_show_label || a!=m_show_above) { |
| 208 | m_show_above = a; |
| 209 | m_show_label = s; |
| 210 | invalidate(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | QSize Field::sizeHint() const { |
| 216 | if (! m_theme) { |
| 217 | return QSize(); |
| 218 | } else { |
| 219 | return QSize(m_theme->getFieldWidth(), m_theme->getFieldHeight()); |
| 220 | } |
| 221 | } |
| 222 | */ |
| 223 | |
| 224 | /* |
| 225 | void Field::resizeEvent(QResizeEvent* e) { |
| 226 | invalidate(); |
| 227 | if (e->size().width() == 0 || e->size().height() == 0) { |
| 228 | return; |
| 229 | } |
| 230 | draw(); |
| 231 | } |
| 232 | */ |
| 233 |