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