File indexing completed on 2024-12-22 04:18:17
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "colorpalette.h" 0014 0015 #include <QPixmap> 0016 #include <QPainter> 0017 0018 #include "palette.h" 0019 0020 namespace Kst { 0021 0022 ColorPalette::ColorPalette(QWidget *parent) 0023 : QWidget(parent) { 0024 setupUi(this); 0025 0026 connect(_palette, SIGNAL(highlighted(QString)), this, SLOT(updatePalette(QString))); 0027 connect(_palette, SIGNAL(currentIndexChanged(QString)), this, SLOT(updatePalette(QString))); 0028 connect(_palette, SIGNAL(currentIndexChanged(int)), this, SIGNAL(selectionChanged())); 0029 0030 refresh(DefaultPalette); 0031 updatePalette(DefaultPalette); 0032 } 0033 0034 0035 ColorPalette::~ColorPalette() {} 0036 0037 0038 void ColorPalette::updatePalette(const QString &palette) { 0039 QString paletteName = palette; 0040 if (!paletteName.isEmpty()) { 0041 paletteName = selectedPalette(); 0042 } 0043 0044 Palette* newPalette = new Palette(palette); 0045 int numberOfColors = newPalette->colorCount(); 0046 int height = _palette->height()?_palette->height():1; 0047 int width = 7 * height; 0048 0049 QPixmap pix(width, height); 0050 QPainter p(&pix); 0051 0052 p.fillRect(p.window(), QColor("white")); 0053 for (int i=0; i<width; i++) { 0054 int j = i*numberOfColors/width; 0055 p.fillRect(i, 0, 1, height, QBrush(newPalette->color(j))); 0056 } 0057 0058 _paletteDisplay->setPixmap(pix); 0059 0060 delete newPalette; 0061 } 0062 0063 0064 QString ColorPalette::selectedPalette() { 0065 return _palette->currentText(); 0066 } 0067 0068 0069 void ColorPalette::setPalette(const QString palette) { 0070 _palette->setCurrentIndex(_palette->findText(palette)); 0071 updatePalette(palette); 0072 } 0073 0074 0075 bool ColorPalette::selectedPaletteDirty() const { 0076 return _palette->currentIndex() != -1; 0077 } 0078 0079 0080 void ColorPalette::clearSelection() { 0081 _palette->setCurrentIndex(-1); 0082 0083 QPixmap pix(7 *_palette->height(), _palette->height()); 0084 QPainter p(&pix); 0085 p.fillRect(p.window(), QColor("white")); 0086 _paletteDisplay->setPixmap(pix); 0087 } 0088 0089 0090 void ColorPalette::refresh( const QString & palette ) { 0091 _palette->clear(); 0092 0093 QStringList paletteList = Palette::getPaletteList(); 0094 paletteList.sort(); 0095 _palette->addItems(paletteList); 0096 0097 if (!palette.isEmpty()) { 0098 int i; 0099 for (i = 0; i < _palette->count(); ++i) { 0100 if (_palette->itemText(i) == palette) { 0101 break; 0102 } 0103 } 0104 if (i == _palette->count()) { 0105 i = _palette->findText(DefaultPalette); 0106 if (i<0) { 0107 i=0; 0108 } 0109 } 0110 _palette->setCurrentIndex(i); 0111 } 0112 } 0113 0114 0115 0116 int ColorPalette::currentPaletteIndex() { 0117 return _palette->currentIndex(); 0118 } 0119 0120 } 0121 0122 // vim: ts=2 sw=2 et