File indexing completed on 2024-03-24 15:48:01

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2000-2016 Klaas Freitag <freitag@suse.de>     *
0007  *                          Jonathan Marten <jjm@keelhaul.me.uk>    *
0008  *                                  *
0009  *  Kooka is free software; you can redistribute it and/or modify it    *
0010  *  under the terms of the GNU Library General Public License as    *
0011  *  published by the Free Software Foundation and appearing in the  *
0012  *  file COPYING included in the packaging of this file;  either    *
0013  *  version 2 of the License, or (at your option) any later version.    *
0014  *                                  *
0015  *  As a special exception, permission is given to link this program    *
0016  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0017  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0018  *  executable without including the source code for KADMOS in the  *
0019  *  source distribution.                        *
0020  *                                  *
0021  *  This program is distributed in the hope that it will be useful, *
0022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0024  *  GNU General Public License for more details.            *
0025  *                                  *
0026  *  You should have received a copy of the GNU General Public       *
0027  *  License along with this program;  see the file COPYING.  If     *
0028  *  not, see <http://www.gnu.org/licenses/>.                *
0029  *                                  *
0030  ************************************************************************/
0031 
0032 #include "gammawidget.h"
0033 
0034 #include <qpainter.h>
0035 #include <qpixmap.h>
0036 #include <qevent.h>
0037 
0038 #include "kgammatable.h"
0039 
0040 
0041 static const int MARGIN = 5;                // margin around display
0042 static const int NGRID = 4;             // number of grid lines
0043 
0044 
0045 GammaWidget::GammaWidget(KGammaTable *table, QWidget *parent)
0046     : QWidget(parent)
0047 {
0048     mTable = table;
0049     connect(mTable, &KGammaTable::tableChanged, this, QOverload<>::of(&QWidget::repaint));
0050 
0051     QSizePolicy pol(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
0052     setSizePolicy(pol);
0053 }
0054 
0055 void GammaWidget::paintEvent(QPaintEvent *ev)
0056 {
0057     QPainter p(this);
0058 
0059     // Calculate the minimum display size
0060     int size = qMin(width(), height()) - 2 * MARGIN;
0061 
0062     // Limit the painting area to a square of that size
0063     p.setViewport(MARGIN, MARGIN, size, size);
0064     p.setWindow(0, 0, size, size);
0065 
0066     // background and outer frame
0067     p.setBrush(palette().base());
0068     p.setPen(palette().windowText().color());
0069     p.drawRect(0, 0, size, size);
0070 
0071     // grid lines
0072     p.setPen(QPen(palette().mid().color(), 0, Qt::DotLine));
0073     int step = size / (NGRID + 1);
0074     for (int l = 1; l <= NGRID; ++l) {
0075         p.drawLine(1, l * step, size - 1, l * step);    // horizontal line
0076         p.drawLine(l * step, 1, l * step, size - 1);    // vertical line
0077     }
0078 
0079     if (mTable == nullptr) return;              // no values to draw
0080 
0081     // the gamma curve
0082     p.setPen(palette().highlight().color());
0083 
0084     const int *vals = mTable->getTable();
0085     const int nvals = mTable->tableSize();
0086     const int maxval = KGammaTable::valueRange;
0087     p.setWindow(0, 0, nvals, maxval);
0088 
0089     int py = maxval-vals[0];
0090     for (int x = 1; x<nvals; ++x)
0091     {
0092         int y = maxval-vals[x];
0093         p.drawLine(x-1, py, x, y);
0094         py = y;
0095     }
0096 }
0097 
0098 QSize GammaWidget::sizeHint() const
0099 {
0100     return (QSize(256 + 2 * MARGIN, 256 + 2 * MARGIN));
0101 }