File indexing completed on 2024-05-19 04:36:34

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "ColorPalette.h"
0021 
0022 #include <tikz/core/Style.h>
0023 #include <tikz/core/tikz.h>
0024 
0025 #include <QDebug>
0026 #include <QFile>
0027 #include <QFileInfo>
0028 #include <QTextStream>
0029 #include <QRegularExpression>
0030 
0031 namespace tikz {
0032 namespace ui {
0033 
0034 class ColorPalettePrivate
0035 {
0036     public:
0037         QVector<QRgb> colors;
0038         QString name;
0039         int rows = 0;
0040         int columns = 0;
0041         QVector<int> spacings;
0042 };
0043 
0044 ColorPalette::ColorPalette()
0045     : d(new ColorPalettePrivate())
0046 {
0047 }
0048 
0049 ColorPalette::~ColorPalette()
0050 {
0051     delete d;
0052 }
0053 
0054 void ColorPalette::load(const QString & filename)
0055 {
0056     // make sure this color palette initially has no name
0057     d->name.clear();
0058     d->rows = 0;
0059     d->columns = 0;
0060     d->spacings.clear();
0061 
0062     QStringList lines;
0063     {
0064         QFile file(filename);
0065         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
0066             return;
0067         QTextStream ts(&file);
0068         lines = ts.readAll().split(QLatin1Char('\n'), Qt::SkipEmptyParts);
0069     }
0070 
0071     static QRegularExpression whiteSpaces("\\s+");
0072 
0073     for (int i = 0; i < lines.size(); ++i) {
0074         const QString & line = lines[i];
0075         // ignore comments
0076         if (line.startsWith(QLatin1Char('#'))) {
0077             continue;
0078         }
0079 
0080         // check for Name:
0081         if (line.startsWith(QLatin1String("Name:"))) {
0082             d->name = line.right(line.size() - 5).trimmed();
0083             continue;
0084         }
0085 
0086         // check for Columns:
0087         if (line.startsWith(QLatin1String("Columns:"))) {
0088             d->columns = line.right(line.size() - 8).trimmed().toInt();
0089             continue;
0090         }
0091 
0092         // check for Spacing: space separated int list. Each int is a row, starting at 0.
0093         if (line.startsWith(QLatin1String("Spacing:"))) {
0094             const QStringList list = line.right(line.size() - 8).trimmed().split(whiteSpaces, Qt::SkipEmptyParts);
0095             for (const QString r : list) {
0096                 d->spacings.append(r.toInt());
0097             }
0098             continue;
0099         }
0100 
0101         // check for "r g b <tab> name"
0102         QStringList color = line.split(whiteSpaces, Qt::SkipEmptyParts);
0103         if (color.size() >= 3) {
0104             d->colors << qRgb(color[0].toInt(),
0105                               color[1].toInt(),
0106                               color[2].toInt());
0107         }
0108     }
0109 
0110     if (d->columns > 0) {
0111         d->rows = d->colors.size() / d->columns;
0112     }
0113 
0114 //     qDebug() << "Color palette" << QFileInfo(filename).fileName() << ":" << d->colors.size() << "colors (" << d->rows << ", " << d->columns << ")";
0115 }
0116 
0117 QString ColorPalette::name() const noexcept
0118 {
0119     return d->name;
0120 }
0121 
0122 QRgb ColorPalette::color(int row, int column) const noexcept
0123 {
0124     Q_ASSERT(row >= 0 && row < d->rows);
0125     Q_ASSERT(column >= 0 && column < d->columns);
0126 
0127     return d->colors[row * d->columns + column];
0128 }
0129 
0130 int ColorPalette::rows() const noexcept
0131 {
0132     return d->rows;
0133 }
0134 
0135 int ColorPalette::columns() const noexcept
0136 {
0137     return d->columns;
0138 }
0139 
0140 bool ColorPalette::spacingAfterRow(int row) const
0141 {
0142     return d->spacings.contains(row);
0143 }
0144 
0145 }
0146 }
0147 
0148 // kate: indent-width 4; replace-tabs on;