55
66#include < core/IconProvider.h>
77
8+ #include < QSize>
9+
10+ #include < algorithm>
811#include < cassert>
912#include < utility>
1013
1417#include " Settings.h"
1518
1619namespace fix_orientation {
20+ namespace {
21+
22+ const int kMinInnerSide = 32 ;
23+
24+ } // namespace
25+
1726OptionsWidget::OptionsWidget (std::shared_ptr<Settings> settings, const PageSelectionAccessor& pageSelectionAccessor)
1827 : m_settings(std::move(settings)),
1928 m_pageSelectionAccessor (pageSelectionAccessor),
@@ -26,12 +35,15 @@ OptionsWidget::OptionsWidget(std::shared_ptr<Settings> settings, const PageSelec
2635
2736OptionsWidget::~OptionsWidget () = default ;
2837
29- void OptionsWidget::preUpdateUI (const PageId& pageId , const OrthogonalRotation rotation) {
38+ void OptionsWidget::preUpdateUI (const PageInfo& pageInfo , const OrthogonalRotation rotation) {
3039 auto block = m_connectionManager.getScopedBlock ();
3140
32- m_pageId = pageId;
41+ m_pageId = pageInfo.id ();
42+ m_imagePixelSize = pageInfo.metadata ().size ();
3343 m_rotation = rotation;
3444 setRotationPixmap ();
45+ updateTrimMaximums ();
46+ pullTrimToControls ();
3547}
3648
3749void OptionsWidget::postUpdateUI (const OrthogonalRotation rotation) {
@@ -100,6 +112,76 @@ void OptionsWidget::setRotation(const OrthogonalRotation& rotation) {
100112 emit invalidateThumbnail (m_pageId);
101113}
102114
115+ void OptionsWidget::trimEnableToggled (const bool checked) {
116+ trimLeftSpin->setEnabled (checked);
117+ trimRightSpin->setEnabled (checked);
118+ trimTopSpin->setEnabled (checked);
119+ trimBottomSpin->setEnabled (checked);
120+ resetTrimBtn->setEnabled (checked);
121+ if (!checked) {
122+ m_settings->clearTrim (m_pageId.imageId ());
123+ emit invalidateThumbnail (m_pageId);
124+ return ;
125+ }
126+ pushTrimFromControls ();
127+ }
128+
129+ void OptionsWidget::trimMarginsChanged (const int ) {
130+ if (!trimEnabledCheck->isChecked ()) {
131+ return ;
132+ }
133+ pushTrimFromControls ();
134+ }
135+
136+ void OptionsWidget::resetTrim () {
137+ auto block = m_connectionManager.getScopedBlock ();
138+ trimLeftSpin->setValue (0 );
139+ trimRightSpin->setValue (0 );
140+ trimTopSpin->setValue (0 );
141+ trimBottomSpin->setValue (0 );
142+ if (trimEnabledCheck->isChecked ()) {
143+ pushTrimFromControls ();
144+ } else {
145+ m_settings->clearTrim (m_pageId.imageId ());
146+ emit invalidateThumbnail (m_pageId);
147+ }
148+ }
149+
150+ void OptionsWidget::updateTrimMaximums () {
151+ const int w = m_imagePixelSize.width ();
152+ const int h = m_imagePixelSize.height ();
153+ const int maxSide = std::max (0 , std::max (w, h) - kMinInnerSide );
154+ trimLeftSpin->setMaximum (maxSide);
155+ trimRightSpin->setMaximum (maxSide);
156+ trimTopSpin->setMaximum (maxSide);
157+ trimBottomSpin->setMaximum (maxSide);
158+ }
159+
160+ void OptionsWidget::pullTrimToControls () {
161+ const ImageTrim trim (m_settings->getTrim (m_pageId.imageId ()));
162+ trimEnabledCheck->setChecked (trim.enabled );
163+ trimLeftSpin->setValue (trim.left );
164+ trimRightSpin->setValue (trim.right );
165+ trimTopSpin->setValue (trim.top );
166+ trimBottomSpin->setValue (trim.bottom );
167+ trimLeftSpin->setEnabled (trim.enabled );
168+ trimRightSpin->setEnabled (trim.enabled );
169+ trimTopSpin->setEnabled (trim.enabled );
170+ trimBottomSpin->setEnabled (trim.enabled );
171+ resetTrimBtn->setEnabled (trim.enabled );
172+ }
173+
174+ void OptionsWidget::pushTrimFromControls () {
175+ ImageTrim trim;
176+ trim.enabled = true ;
177+ trim.left = trimLeftSpin->value ();
178+ trim.right = trimRightSpin->value ();
179+ trim.top = trimTopSpin->value ();
180+ trim.bottom = trimBottomSpin->value ();
181+ m_settings->setTrim (m_pageId.imageId (), trim);
182+ emit invalidateThumbnail (m_pageId);
183+ }
184+
103185void OptionsWidget::setRotationPixmap () {
104186 QIcon icon;
105187 switch (m_rotation.toDegrees ()) {
@@ -128,6 +210,12 @@ void OptionsWidget::setupUiConnections() {
128210 CONNECT (rotateRightBtn, SIGNAL (clicked ()), this , SLOT (rotateRight ()));
129211 CONNECT (resetBtn, SIGNAL (clicked ()), this , SLOT (resetRotation ()));
130212 CONNECT (applyToBtn, SIGNAL (clicked ()), this , SLOT (showApplyToDialog ()));
213+ CONNECT (trimEnabledCheck, SIGNAL (toggled (bool )), this , SLOT (trimEnableToggled (bool )));
214+ CONNECT (trimLeftSpin, SIGNAL (valueChanged (int )), this , SLOT (trimMarginsChanged (int )));
215+ CONNECT (trimRightSpin, SIGNAL (valueChanged (int )), this , SLOT (trimMarginsChanged (int )));
216+ CONNECT (trimTopSpin, SIGNAL (valueChanged (int )), this , SLOT (trimMarginsChanged (int )));
217+ CONNECT (trimBottomSpin, SIGNAL (valueChanged (int )), this , SLOT (trimMarginsChanged (int )));
218+ CONNECT (resetTrimBtn, SIGNAL (clicked ()), this , SLOT (resetTrim ()));
131219}
132220
133221#undef CONNECT
@@ -137,4 +225,4 @@ void OptionsWidget::setupIcons() {
137225 rotateLeftBtn->setIcon (iconProvider.getIcon (" object-rotate-left" ));
138226 rotateRightBtn->setIcon (iconProvider.getIcon (" object-rotate-right" ));
139227}
140- } // namespace fix_orientation
228+ } // namespace fix_orientation
0 commit comments