00001
00007 #include <QtGui/qcolor.h>
00008 #include <QtGui/qdialog.h>
00009 #include <QtGui/qevent.h>
00010 #include <QtGui/qpainter.h>
00011 #include <QtGui/qmenu.h>
00012 #include <limits.h>
00013 #include <math.h>
00014 #include "palette.h"
00015 #include "ui_dqtscale.h"
00016 #include "defs.h"
00017
00018
00019
00020
00021 Palette::Palette(QWidget *parent)
00022 : QWidget( parent)
00023 {
00024 m_vMin = 0;
00025 m_vMax = 8;
00026 CreatePaletteMenu();
00027 m_idxPal = BW;
00028 m_isLogScale = FALSE;
00029 AdjustScaleFactor();
00030 setFixedWidth(50);
00031 }
00032
00033 Palette::~Palette()
00034 {
00035 }
00036
00037 void Palette::paintEvent(QPaintEvent *)
00038 {
00039 QPainter paint(this);
00040 paintPalette(&paint);
00041 }
00042
00043 void Palette::paintPalette(QPainter *p)
00044 {
00045 QFontMetrics fm = p->fontMetrics();
00046 int htText = fm.height();
00047 int mg = 2;
00048
00049 QString s;
00050 s.setNum(m_vMin);
00051 p->drawText(width() - fm.width(s) - mg, height(), s);
00052
00053 s.setNum(m_vMax);
00054 int wMax = fm.width(s);
00055 p->drawText(width() - wMax - mg, htText, s);
00056
00057 int y = 0;
00058 int yPrec = -1;
00059
00060 QRect rectPal = QRect(0, 0, width() - wMax - 2*mg, height());
00061
00062 for (unsigned int i = 0; i <= NB_COLORS; i++)
00063 {
00064 y = rectPal.height() * i / NB_COLORS;
00065 p->fillRect(rectPal.left(),
00066 y + rectPal.top(),
00067 rectPal.width(),
00068 y - yPrec + 1,
00069 GetColor(GetPalIndex(NB_COLORS-i)));
00070 yPrec = y;
00071 }
00072
00073 int nbTicks = 16;
00074 bool found = FALSE;
00075 while (nbTicks > 2 && !found) {
00076 nbTicks--;
00077 if ((m_vMax - m_vMin) % nbTicks == 0) {
00078 found = TRUE;
00079 }
00080 }
00081 if (!found) {
00082 nbTicks = 10;
00083 }
00084 int x1 = rectPal.right();
00085 int x2 = x1 - rectPal.width()/2;
00086
00087 for (int i = 1; i < nbTicks; i++) {
00088 int val = (m_vMax - (m_vMax - m_vMin) * i / nbTicks);
00089 y = rectPal.bottom() - rectPal.height() * (val - m_vMin) / (m_vMax - m_vMin);
00090 p->drawLine(x1, y, x2, y);
00091 s.setNum(val);
00092 p->drawText(width() - fm.width(s) - mg, y + htText/2, s);
00093 }
00094 }
00095
00096 int Palette::GetPalIndex(int i) const
00097 {
00098 if (m_isLogScale) {
00099 return (int)(log ((double)(1 + i)) * UCHAR_MAX / log((double)(1 + UCHAR_MAX)));
00100 }
00101 return i;
00102 }
00103
00104 int Palette::DataToIndex(int val)
00105 {
00106 if (val > m_vMax) {
00107 return UCHAR_MAX;
00108 } else if (val <= m_vMin) {
00109 return 0;
00110 } else if (!m_isLogScale) {
00111 return (int)((val - m_vMin) * m_fact);
00112 } else {
00113 return (int)(log ((double)(1 + val - m_vMin)) * m_fact);
00114 }
00115 }
00116
00117 QColor Palette::GetColor(int i) const
00118 {
00119 QColor c;
00120
00121 switch (m_idxPal)
00122 {
00123 case BW :
00124 if (!i) {
00125 c = Qt::white;
00126 }
00127 else {
00128 int ind = (NB_COLORS-i)*256/NB_COLORS;
00129 c.setRgb(ind, ind, ind);
00130 }
00131 break;
00132 case COLOR :
00133 c.setHsv((NB_COLORS-i)*240/NB_COLORS, 255, 255);
00134 break;
00135 }
00136 return c;
00137 }
00138
00139 void Palette::AdjustScaleFactor()
00140 {
00141 if (m_isLogScale) {
00142 m_fact = (float)UCHAR_MAX / log(1 + (float)(m_vMax - m_vMin));
00143 } else {
00144 m_fact = (float)UCHAR_MAX / (float)(m_vMax - m_vMin);
00145 }
00146 }
00147
00148 void Palette::CreatePaletteMenu()
00149 {
00150 m_menuPalette = new QMenu("Display");
00151
00152 m_menuColors = new QMenu("&Palette");
00153 m_menuColors->addAction("&Gray scale", this, SLOT(SelectBW()));
00154 m_menuColors->addAction("&Color", this, SLOT(SelectColor()));
00155 m_menuPalette->addMenu(m_menuColors);
00156
00157 m_menuLinLog = new QMenu("&Scale");
00158 m_menuLinLog->addAction("&Linear", this, SLOT(SelectLinear()));
00159 m_menuLinLog->addAction("&Logarithmic", this, SLOT(SelectLogarithmic()));
00160 m_menuPalette->addMenu(m_menuLinLog);
00161
00162 QMenu* menuScale = new QMenu("&Adjust scale");
00163 menuScale->addAction("&Manual", this, SLOT(AdjustScale()));
00164 menuScale->addAction("&Automatic", this, SLOT(AdjustAutoScale()));
00165 m_menuPalette->addMenu(menuScale);
00166 }
00167
00168 void Palette::SelectBW()
00169 {
00170 ChangePalette(BW);
00171 }
00172
00173 void Palette::SelectColor()
00174 {
00175 ChangePalette(COLOR);
00176 }
00177
00178 void Palette::ChangePalette(int iPal)
00179 {
00180 if (iPal == m_idxPal) {
00181 return;
00182 }
00183 m_idxPal = iPal;
00184 update();
00185 emit(PaletteModified());
00186 }
00187
00188 void Palette::SelectLinear()
00189 {
00190 ChangeLinLog(FALSE);
00191 }
00192 void Palette::SelectLogarithmic()
00193 {
00194 ChangeLinLog(TRUE);
00195 }
00196
00197 void Palette::ChangeLinLog(bool type)
00198 {
00199 if (type == m_isLogScale) {
00200 return;
00201 }
00202 m_isLogScale = type;
00203 AdjustScaleFactor();
00204 update();
00205 emit(PaletteModified());
00206 }
00207
00208 void Palette::mousePressEvent(QMouseEvent *e)
00209 {
00210 if (e->button() != Qt::RightButton) {
00211 return;
00212 }
00213 m_menuPalette->popup(e->globalPos());
00214 }
00215
00216 void Palette::ChangeExtrema(int vMin, int vMax)
00217 {
00218 if (vMin < vMax) {
00219 m_vMin = vMin;
00220 m_vMax = vMax;
00221 AdjustScaleFactor();
00222 repaint();
00223 emit(PaletteModified());
00224 }
00225 }
00226
00227 void Palette::ChangeDataMinMax(int min, int max)
00228 {
00229 ChangeExtrema(min, max);
00230 m_dataMin = min;
00231 m_dataMax = max;
00232 }
00233
00234 void Palette::AdjustScale()
00235 {
00236 QDialog *window = new QDialog;
00237 Ui::DqtScale dscale;
00238 dscale.setupUi(window);
00239 dscale.m_spMin->setValue(m_vMin);
00240 dscale.m_spMax->setValue(m_vMax);
00241 if (window->exec()) {
00242 ChangeExtrema(dscale.m_spMin->value(), dscale.m_spMax->value());
00243 }
00244 delete window;
00245 }
00246
00247 void Palette::AdjustAutoScale()
00248 {
00249 ChangeExtrema(m_dataMin,m_dataMax);
00250 }
00251
00252 int Palette::GetPal() const {
00253 return m_idxPal;
00254 }
00255
00256 int Palette::GetMin() const {
00257 return m_vMin;
00258 }
00259
00260 int Palette::GetMax() const {
00261 return m_vMax;
00262 }
00263
00264 bool Palette::IsLogScale() const {
00265 return m_isLogScale;
00266 }
00267
00268 QMenu* Palette::GetMenu() const {
00269 return m_menuPalette;
00270 }
00271