| 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 <QSettings> |
| 24 | #include <QPainter> |
| 25 | #include <QSvgRenderer> |
| 26 | #include <QMouseEvent> |
| 27 | #include <QDebug> |
| 28 | #include <QFileInfo> |
| 29 | |
| 30 | #include "theme.h" |
| 31 | #include "common.h" |
| 32 | #include "checkers.h" |
| 33 | |
| 34 | Theme::Theme(QObject* parent, const QString& path, int size) : QObject(parent) { |
| 35 | m_path = path; |
| 36 | |
| 37 | settings = new QSettings(path + "/"THEME_FILE, QSettings::IniFormat, this); |
| 38 | qInfo("Theme: %s\n", qUtf8Printable(name())); |
| 39 | |
| 40 | if (path == DEFAULT_THEME) { |
| 41 | // just in case no themes installed. |
| 42 | m_pat1_path = ":/icons/theme/tile1.png"; |
| 43 | m_pat2_path = ":/icons/theme/tile2.png"; |
| 44 | m_frame_path = ":/icons/theme/frame.png"; |
| 45 | |
| 46 | m_man_black_path = ":/icons/theme/manblack.png"; |
| 47 | m_man_white_path = ":/icons/theme/manwhite.png"; |
| 48 | m_king_black_path = ":/icons/theme/kingblack.png"; |
| 49 | m_king_white_path = ":/icons/theme/kingwhite.png"; |
| 50 | } else { |
| 51 | m_pat1_path = path+"/" + settings->value("tile1", THEME_TILE1).toString(); |
| 52 | m_pat2_path = path+"/" + settings->value("tile2", THEME_TILE2).toString(); |
| 53 | m_frame_path = path+"/" + settings->value("frame", THEME_FRAME).toString(); |
| 54 | |
| 55 | m_man_black_path = path+"/" + settings->value("man_black", THEME_MANBLACK).toString(); |
| 56 | m_man_white_path = path+"/" + settings->value("man_white", THEME_MANWHITE).toString(); |
| 57 | m_king_black_path = path+"/" + settings->value("king_black", THEME_KINGBLACK).toString(); |
| 58 | m_king_white_path = path+"/" + settings->value("king_white", THEME_KINGWHITE).toString(); |
| 59 | } |
| 60 | m_target_size = size; |
| 61 | //m_target_size = size == 0 ? MAX_TILE_SIZE : size; |
| 62 | |
| 63 | reset(); |
| 64 | } |
| 65 | |
| 66 | void Theme::reset() { |
| 67 | m_man_white_valid = false; |
| 68 | m_man_black_valid = false; |
| 69 | m_king_black_valid = false; |
| 70 | m_king_white_valid = false; |
| 71 | m_pattern1_valid = false; |
| 72 | m_pattern2_valid = false; |
| 73 | m_frame_valid = false; |
| 74 | } |
| 75 | |
| 76 | bool Theme::isValid() { |
| 77 | if (m_path == DEFAULT_THEME) { |
| 78 | return true; |
| 79 | } else { |
| 80 | if (! QFileInfo::exists(m_pat1_path) || |
| 81 | ! QFileInfo::exists(m_pat2_path) || |
| 82 | ! QFileInfo::exists(m_frame_path) || |
| 83 | ! QFileInfo::exists(m_man_black_path) || |
| 84 | ! QFileInfo::exists(m_man_white_path) || |
| 85 | ! QFileInfo::exists(m_king_white_path) || |
| 86 | ! QFileInfo::exists(m_king_black_path)) { |
| 87 | qWarning("one of required files does not exist. "); |
| 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const QString Theme::name() { |
| 95 | QFileInfo info(m_path); |
| 96 | return settings->value("name", info.baseName()).toString(); |
| 97 | } |
| 98 | |
| 99 | QPixmap& Theme::getFrame() { |
| 100 | render(m_frame_path, m_frame, m_frame_valid); |
| 101 | return m_frame; |
| 102 | } |
| 103 | |
| 104 | QPixmap& Theme::getPattern1() { |
| 105 | render(m_pat1_path, m_pattern1, m_pattern1_valid); |
| 106 | return m_pattern1; |
| 107 | } |
| 108 | |
| 109 | QPixmap& Theme::getPattern2() { |
| 110 | render(m_pat2_path, m_pattern2, m_pattern2_valid); |
| 111 | return m_pattern2; |
| 112 | } |
| 113 | |
| 114 | QPixmap& Theme::getPattern(int i) { |
| 115 | if (i == 1) { |
| 116 | return getPattern1(); |
| 117 | } else { |
| 118 | return getPattern2(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | QPixmap& Theme::getMan1(bool white) { |
| 123 | if (white) { |
| 124 | render(m_man_white_path, m_man_white, m_man_white_valid); |
| 125 | return m_man_white; |
| 126 | } else { |
| 127 | render(m_man_black_path, m_man_black, m_man_black_valid); |
| 128 | return m_man_black; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | QPixmap& Theme::getMan2(bool white) { |
| 133 | if (! white) { |
| 134 | render(m_man_white_path, m_man_white, m_man_white_valid); |
| 135 | return m_man_white; |
| 136 | } else { |
| 137 | render(m_man_black_path, m_man_black, m_man_black_valid); |
| 138 | return m_man_black; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | QPixmap& Theme::getKing1(bool white) { |
| 143 | if (white) { |
| 144 | render(m_king_white_path, m_king_white, m_king_white_valid); |
| 145 | return m_king_white; |
| 146 | } else { |
| 147 | render(m_king_black_path, m_king_black, m_king_black_valid); |
| 148 | return m_king_black; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | QPixmap& Theme::getKing2(bool white) { |
| 153 | if (! white) { |
| 154 | render(m_king_white_path, m_king_white, m_king_white_valid); |
| 155 | return m_king_white; |
| 156 | } else { |
| 157 | render(m_king_black_path, m_king_black, m_king_black_valid); |
| 158 | return m_king_black; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | QPixmap& Theme::getChecker(int item, bool white) { |
| 163 | switch (item) { |
| 164 | case MAN1: |
| 165 | return getMan1(white); |
| 166 | case MAN2: |
| 167 | return getMan2(white); |
| 168 | case KING1: |
| 169 | return getKing1(white); |
| 170 | case KING2: |
| 171 | return getKing2(white); |
| 172 | default: |
| 173 | return m_empty_pixmap; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int Theme::getFieldWidth() { |
| 178 | render(m_man_white_path, m_man_white, m_man_white_valid); |
| 179 | int result = m_man_white.width(); |
| 180 | return result; |
| 181 | //return result == 0 ? MAX_TILE_SIZE : result; |
| 182 | } |
| 183 | |
| 184 | int Theme::getFieldHeight() { |
| 185 | render(m_man_white_path, m_man_white, m_man_white_valid); |
| 186 | int result = m_man_white.height(); |
| 187 | return result; |
| 188 | //return result == 0 ? MAX_TILE_SIZE : result; |
| 189 | } |
| 190 | |
| 191 | void Theme::setTargetSize(int size) { |
| 192 | /*if (size == 0) { |
| 193 | size = MAX_TILE_SIZE; |
| 194 | qInfo("Size is 0, set it to %d", size); |
| 195 | }*/ |
| 196 | if (m_target_size != size) { |
| 197 | qInfo("Size: %d -> %d", m_target_size, size); |
| 198 | m_target_size = size; |
| 199 | |
| 200 | // reset cached pixmaps, |
| 201 | // so that they will be re-rendered |
| 202 | reset(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | bool Theme::getIsResizeable() { |
| 207 | // FIXME |
| 208 | return m_man_white_path.endsWith(".svg"); |
| 209 | } |
| 210 | |
| 211 | void Theme::render(const QString& path, QPixmap& result, bool& valid) { |
| 212 | if (valid) { |
| 213 | //qDebug("Already valid: %s", qUtf8Printable(path)); |
| 214 | return; |
| 215 | } |
| 216 | int size = m_target_size; |
| 217 | valid = true; |
| 218 | if (path.endsWith(".svg")) { |
| 219 | QSvgRenderer renderer(path); |
| 220 | if (size == 0) { |
| 221 | size = renderer.defaultSize().width(); |
| 222 | } |
| 223 | qInfo("Rendering %s: %d", qUtf8Printable(path), size); |
| 224 | Q_ASSERT(size > 0); |
| 225 | result = QPixmap(size, size); |
| 226 | result.fill(Qt::transparent); |
| 227 | QPainter paint(&result); |
| 228 | renderer.render(&paint); |
| 229 | |
| 230 | } else { |
| 231 | qInfo("Rendering %s: %d", qUtf8Printable(path), size); |
| 232 | if (size == 0) { |
| 233 | result = QPixmap(path); |
| 234 | } else { |
| 235 | Q_ASSERT(size > 0); |
| 236 | result = QPixmap(path).scaled(size, size); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 |