00001
00002
00003
00004
00005
00006
00007 #include "CmdMediator.h"
00008 #include "CmdSettingsPointMatch.h"
00009 #include "DlgSettingsPointMatch.h"
00010 #include "EngaugeAssert.h"
00011 #include "Logger.h"
00012 #include "MainWindow.h"
00013 #include <QComboBox>
00014 #include <QGraphicsEllipseItem>
00015 #include <QGraphicsPixmapItem>
00016 #include <QGraphicsRectItem>
00017 #include <QGraphicsScene>
00018 #include <QGridLayout>
00019 #include <QLabel>
00020 #include <qmath.h>
00021 #include <QPen>
00022 #include <QSpinBox>
00023 #include "ViewPreview.h"
00024
00025 const int MINIMUM_HEIGHT = 480;
00026 const int POINT_SIZE_MAX = 1024;
00027 const int POINT_SIZE_MIN = 5;
00028
00029 DlgSettingsPointMatch::DlgSettingsPointMatch(MainWindow &mainWindow) :
00030 DlgSettingsAbstractBase (tr ("Point Match"),
00031 "DlgSettingsPointMatch",
00032 mainWindow),
00033 m_scenePreview (0),
00034 m_viewPreview (0),
00035 m_circle (0),
00036 m_modelPointMatchBefore (0),
00037 m_modelPointMatchAfter (0)
00038 {
00039 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::DlgSettingsPointMatch";
00040
00041 QWidget *subPanel = createSubPanel ();
00042 finishPanel (subPanel);
00043 }
00044
00045 DlgSettingsPointMatch::~DlgSettingsPointMatch()
00046 {
00047 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::~DlgSettingsPointMatch";
00048 }
00049
00050 QPointF DlgSettingsPointMatch::boxPositionConstraint(const QPointF &posIn)
00051 {
00052 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::boxPositionConstraint";
00053
00054 double radius = radiusAlongDiagonal();
00055 double diameter = 2.0 * radius;
00056
00057
00058 QPointF pos (posIn);
00059 if (pos.x() - radius < 0) {
00060 pos.setX (radius);
00061 }
00062
00063 if (pos.y() - radius < 0) {
00064 pos.setY (radius);
00065 }
00066
00067 if (pos.x() + diameter > m_scenePreview->sceneRect().width ()) {
00068 pos.setX (m_scenePreview->sceneRect().width() - diameter);
00069 }
00070
00071 if (pos.y() + diameter > m_scenePreview->sceneRect().height ()) {
00072 pos.setY (m_scenePreview->sceneRect().height() - diameter);
00073 }
00074
00075 return pos;
00076 }
00077
00078 void DlgSettingsPointMatch::createControls (QGridLayout *layout,
00079 int &row)
00080 {
00081 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::createControls";
00082
00083 QLabel *labelPointSize = new QLabel (tr ("Maximum point size (pixels):"));
00084 layout->addWidget (labelPointSize, row, 1);
00085
00086 m_spinPointSize = new QSpinBox;
00087 m_spinPointSize->setWhatsThis (tr ("Select a maximum point size in pixels.\n\n"
00088 "Sample match points must fit within a square box, around the cursor, having width and height "
00089 "equal to this maximum.\n\n"
00090 "This size is also used to determine if a region of pixels that are on, in the processed image, "
00091 "should be ignored since that region is wider or taller than this limit.\n\n"
00092 "This value has a lower limit"));
00093 m_spinPointSize->setMinimum (POINT_SIZE_MIN);
00094 m_spinPointSize->setMaximum (POINT_SIZE_MAX);
00095 connect (m_spinPointSize, SIGNAL (valueChanged (int)), this, SLOT (slotMaxPointSize (int)));
00096 layout->addWidget (m_spinPointSize, row++, 2);
00097
00098 QLabel *labelAcceptedPointColor = new QLabel (tr ("Accepted point color:"));
00099 layout->addWidget (labelAcceptedPointColor, row, 1);
00100
00101 m_cmbAcceptedPointColor = new QComboBox;
00102 m_cmbAcceptedPointColor->setWhatsThis (tr ("Select a color for matched points that are accepted"));
00103 populateColorComboWithTransparent (*m_cmbAcceptedPointColor);
00104 connect (m_cmbAcceptedPointColor, SIGNAL (activated (const QString &)), this, SLOT (slotAcceptedPointColor (const QString &)));
00105 layout->addWidget (m_cmbAcceptedPointColor, row++, 2);
00106
00107 QLabel *labelRejectedPointColor = new QLabel (tr ("Rejected point color:"));
00108 layout->addWidget (labelRejectedPointColor, row, 1);
00109
00110 m_cmbRejectedPointColor = new QComboBox;
00111 m_cmbRejectedPointColor->setWhatsThis (tr ("Select a color for matched points that are rejected"));
00112 populateColorComboWithTransparent (*m_cmbRejectedPointColor);
00113 connect (m_cmbRejectedPointColor, SIGNAL (activated (const QString &)), this, SLOT (slotRejectedPointColor (const QString &)));
00114 layout->addWidget (m_cmbRejectedPointColor, row++, 2);
00115
00116 QLabel *labelCandidatePointColor = new QLabel (tr ("Candidate point color:"));
00117 layout->addWidget (labelCandidatePointColor, row, 1);
00118
00119 m_cmbCandidatePointColor = new QComboBox;
00120 m_cmbCandidatePointColor->setWhatsThis (tr ("Select a color for the point being decided upon"));
00121 populateColorComboWithTransparent (*m_cmbCandidatePointColor);
00122 connect (m_cmbCandidatePointColor, SIGNAL (activated (const QString &)), this, SLOT (slotCandidatePointColor (const QString &)));
00123 layout->addWidget (m_cmbCandidatePointColor, row++, 2);
00124 }
00125
00126 void DlgSettingsPointMatch::createOptionalSaveDefault (QHBoxLayout * )
00127 {
00128 }
00129
00130 void DlgSettingsPointMatch::createPreview (QGridLayout *layout,
00131 int &row)
00132 {
00133 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::createPreview";
00134
00135 QLabel *labelPreview = new QLabel (tr ("Preview"));
00136 layout->addWidget (labelPreview, row++, 0, 1, 4);
00137
00138 m_scenePreview = new QGraphicsScene (this);
00139 m_viewPreview = new ViewPreview (m_scenePreview,
00140 ViewPreview::VIEW_ASPECT_RATIO_VARIABLE,
00141 this);
00142 m_viewPreview->setWhatsThis (tr ("Preview window shows how current settings affect "
00143 "point matching, and how the marked and candidate points are displayed.\n\nThe points are separated "
00144 "by the point separation value, and the maximum point size is shown as a box in the center"));
00145 m_viewPreview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00146 m_viewPreview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00147 m_viewPreview->setMinimumHeight (MINIMUM_PREVIEW_HEIGHT);
00148 connect (m_viewPreview, SIGNAL (signalMouseMove (QPointF)), this, SLOT (slotMouseMove (QPointF)));
00149
00150 layout->addWidget (m_viewPreview, row++, 0, 1, 4);
00151 }
00152
00153 QWidget *DlgSettingsPointMatch::createSubPanel ()
00154 {
00155 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::createSubPanel";
00156
00157 QWidget *subPanel = new QWidget ();
00158 QGridLayout *layout = new QGridLayout (subPanel);
00159 subPanel->setLayout (layout);
00160
00161 layout->setColumnStretch(0, 1);
00162 layout->setColumnStretch(1, 0);
00163 layout->setColumnStretch(2, 0);
00164 layout->setColumnStretch(3, 1);
00165
00166 int row = 0;
00167 createControls (layout, row);
00168 createPreview (layout, row);
00169 createTemplate ();
00170
00171 return subPanel;
00172 }
00173
00174 void DlgSettingsPointMatch::createTemplate ()
00175 {
00176 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::createTemplate";
00177
00178 QPen pen (QBrush (Qt::black), 0);
00179
00180 m_circle = new QGraphicsEllipseItem;
00181 m_circle->setPen (pen);
00182 m_circle->setZValue (100);
00183 m_scenePreview->addItem (m_circle);
00184 }
00185
00186 void DlgSettingsPointMatch::handleOk ()
00187 {
00188 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::handleOk";
00189
00190 CmdSettingsPointMatch *cmd = new CmdSettingsPointMatch (mainWindow (),
00191 cmdMediator ().document(),
00192 *m_modelPointMatchBefore,
00193 *m_modelPointMatchAfter);
00194 cmdMediator ().push (cmd);
00195
00196 hide ();
00197 }
00198
00199 void DlgSettingsPointMatch::initializeBox ()
00200 {
00201 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::initializeBox";
00202
00203 m_circle->setPos (cmdMediator().document().pixmap().width () / 2.0,
00204 cmdMediator().document().pixmap().height () / 2.0);
00205 }
00206
00207 void DlgSettingsPointMatch::load (CmdMediator &cmdMediator)
00208 {
00209 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::load";
00210
00211 setCmdMediator (cmdMediator);
00212
00213
00214 if (m_modelPointMatchBefore != 0) {
00215 delete m_modelPointMatchBefore;
00216 }
00217 if (m_modelPointMatchAfter != 0) {
00218 delete m_modelPointMatchAfter;
00219 }
00220
00221
00222 m_modelPointMatchBefore = new DocumentModelPointMatch (cmdMediator.document());
00223 m_modelPointMatchAfter = new DocumentModelPointMatch (cmdMediator.document());
00224
00225
00226 ENGAUGE_ASSERT (POINT_SIZE_MIN <= m_modelPointMatchAfter->maxPointSize());
00227 ENGAUGE_ASSERT (POINT_SIZE_MAX > m_modelPointMatchAfter->maxPointSize());
00228
00229
00230 m_spinPointSize->setValue(m_modelPointMatchAfter->maxPointSize());
00231
00232 int indexAccepted = m_cmbAcceptedPointColor->findData(QVariant(m_modelPointMatchAfter->paletteColorAccepted()));
00233 ENGAUGE_ASSERT (indexAccepted >= 0);
00234 m_cmbAcceptedPointColor->setCurrentIndex(indexAccepted);
00235
00236 int indexCandidate = m_cmbCandidatePointColor->findData(QVariant(m_modelPointMatchAfter->paletteColorCandidate()));
00237 ENGAUGE_ASSERT (indexCandidate >= 0);
00238 m_cmbCandidatePointColor->setCurrentIndex(indexCandidate);
00239
00240 int indexRejected = m_cmbRejectedPointColor->findData(QVariant(m_modelPointMatchAfter->paletteColorRejected()));
00241 ENGAUGE_ASSERT (indexRejected >= 0);
00242 m_cmbRejectedPointColor->setCurrentIndex(indexRejected);
00243
00244 initializeBox ();
00245
00246
00247 QGraphicsRectItem *boundary = m_scenePreview->addRect (QRect (0,
00248 0,
00249 cmdMediator.document().pixmap().width (),
00250 cmdMediator.document().pixmap().height ()));
00251 boundary->setVisible (false);
00252
00253 m_scenePreview->addPixmap (cmdMediator.document().pixmap());
00254
00255 updateControls();
00256 enableOk (false);
00257 updatePreview();
00258 }
00259
00260 double DlgSettingsPointMatch::radiusAlongDiagonal () const
00261 {
00262 double maxPointSize = m_modelPointMatchAfter->maxPointSize();
00263
00264 return qSqrt (2.0) * maxPointSize / 2.0;
00265 }
00266
00267 void DlgSettingsPointMatch::setSmallDialogs(bool smallDialogs)
00268 {
00269 if (!smallDialogs) {
00270 setMinimumHeight (MINIMUM_HEIGHT);
00271 }
00272 }
00273
00274 void DlgSettingsPointMatch::slotAcceptedPointColor (const QString &)
00275 {
00276 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::slotAcceptedPointColor";
00277
00278 m_modelPointMatchAfter->setPaletteColorAccepted((ColorPalette) m_cmbAcceptedPointColor->currentData().toInt());
00279
00280 updateControls();
00281 updatePreview();
00282 }
00283
00284 void DlgSettingsPointMatch::slotCandidatePointColor (const QString &)
00285 {
00286 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::slotCandidatePointColor";
00287
00288 m_modelPointMatchAfter->setPaletteColorCandidate((ColorPalette) m_cmbCandidatePointColor->currentData().toInt());
00289 updateControls();
00290 updatePreview();
00291 }
00292
00293 void DlgSettingsPointMatch::slotMaxPointSize (int maxPointSize)
00294 {
00295 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::slotMaxPointSize";
00296
00297 m_modelPointMatchAfter->setMaxPointSize(maxPointSize);
00298 updateControls();
00299 updatePreview();
00300 }
00301
00302 void DlgSettingsPointMatch::slotMouseMove (QPointF pos)
00303 {
00304
00305
00306 pos = boxPositionConstraint (pos);
00307
00308 m_circle->setPos (pos);
00309 }
00310
00311 void DlgSettingsPointMatch::slotRejectedPointColor (const QString &)
00312 {
00313 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsPointMatch::slotRejectedPointColor";
00314
00315 m_modelPointMatchAfter->setPaletteColorRejected((ColorPalette) m_cmbRejectedPointColor->currentData().toInt());
00316 updateControls();
00317 updatePreview();
00318 }
00319
00320 void DlgSettingsPointMatch::updateControls()
00321 {
00322
00323 enableOk (true);
00324 }
00325
00326 void DlgSettingsPointMatch::updatePreview()
00327 {
00328
00329 double maxPointSize = m_modelPointMatchAfter->maxPointSize();
00330
00331 double xLeft = -1.0 * maxPointSize / 2.0;
00332 double yTop = -1.0 * maxPointSize / 2.0;
00333
00334
00335 m_circle->setRect (xLeft,
00336 yTop,
00337 maxPointSize,
00338 maxPointSize);
00339 }