Used a group of toggle buttons instead a switcher to choose
[enlarge-shrink-plugin-for-gallery:enlarge-shrink-plugin-for-gallery.git] / galleryenlargeshrinkplugin / galleryenlargeshrinkwidget.cpp
1 /*
2  * galleryenlargeshrinkwidget.cpp
3  *
4  * Copyright (C) 2012 Igalia, S.L.
5  * Author: Antia Puentes <apuentes@igalia.com>
6  *
7  * This file is part of the Gallery Enlarge/Shrink Plugin.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see http://www.gnu.org/licenses/ *
21  */
22
23 #include "galleryenlargeshrinkwidget.h"
24 #include "galleryenlargeshrinkwidget_p.h"
25 #include "galleryenlargeshrinkaboutwidget.h"
26
27 #include <MApplication>
28 #include <MWidgetCreator>
29 #include <MLocale>
30 #include <MLayout>
31 #include <MGridLayoutPolicy>
32 #include <MLabel>
33 #include <MButton>
34 #include <MButtonGroup>
35 #include <MSlider>
36 #include <MSeparator>
37
38 #define ENLARGE_BUTTON_ID  1
39 #define SHRINK_BUTTON_ID   2
40
41 M_REGISTER_WIDGET_NO_CREATE(GalleryEnlargeShrinkWidget)
42
43 GalleryEnlargeShrinkWidgetPrivate::GalleryEnlargeShrinkWidgetPrivate():
44     m_enlargeButton(0),
45     m_shrinkButton(0),
46     m_effectButtonGroup(new MButtonGroup),
47     m_radiusSlider(0),
48     m_radiusLabel(0),
49     m_landscapePolicy(0),
50     m_portraitPolicy(0),
51     m_aboutSeparator(new MSeparator),
52     m_aboutWidget(new GalleryEnlargeShrinkAboutWidget)
53 {
54 }
55
56 GalleryEnlargeShrinkWidgetPrivate::~GalleryEnlargeShrinkWidgetPrivate()
57 {
58     delete m_enlargeButton;
59     delete m_shrinkButton;
60     delete m_effectButtonGroup;
61     delete m_radiusSlider;
62     delete m_radiusLabel;
63     delete m_landscapePolicy;
64     delete m_portraitPolicy;
65     delete m_aboutSeparator;
66     delete m_aboutWidget;
67 }
68
69 GalleryEnlargeShrinkWidget::GalleryEnlargeShrinkWidget(QGraphicsItem* parent) :
70     MWidgetController(parent),
71     d_ptr(new GalleryEnlargeShrinkWidgetPrivate)
72 {
73     setObjectName("GalleryEnlargeShrinkWidget");
74     setStyleName("EnlargeShrinkEditorControl");
75     setContentsMargins(0, 0, 0, 0);
76
77     Q_D(GalleryEnlargeShrinkWidget);
78     d->m_aboutSeparator->setStyleName("CommonHeaderDividerInverted");
79     connect(d->m_aboutWidget, SIGNAL(linkActivated(QString)),
80             this, SIGNAL(aboutLinkActivated(QString)));
81
82
83     d->m_enlargeButton = new MButton("Enlarge");
84     d->m_enlargeButton->setObjectName("GalleryEnlargeButton");
85     d->m_enlargeButton->setStyleName("CommonTopSplitButtonInverted");
86     d->m_enlargeButton->setViewType(MButton::toggleType);
87     d->m_enlargeButton->setCheckable(true);
88     d->m_shrinkButton = new MButton("Shrink");
89     d->m_shrinkButton->setObjectName("GalleryShrinkButton");
90     d->m_shrinkButton->setStyleName("CommonBottomSplitButtonInverted");
91     d->m_shrinkButton->setViewType(MButton::toggleType);
92     d->m_shrinkButton->setCheckable(true);
93     d->m_effectButtonGroup->setExclusive(true);
94     d->m_effectButtonGroup->addButton(d->m_enlargeButton, ENLARGE_BUTTON_ID);
95     d->m_effectButtonGroup->addButton(d->m_shrinkButton,  SHRINK_BUTTON_ID);
96     d->m_enlargeButton->click();
97
98     d->m_radiusLabel = new MLabel("Radius");
99     d->m_radiusLabel->setObjectName("GalleryEnlargeShrinkRadiusLabel");
100     d->m_radiusLabel->setStyleName("CommonSingleTitleInverted");
101     d->m_radiusSlider = new MSlider();
102     d->m_radiusSlider->setObjectName("GalleryEnlargeShrinkRadiusSlider");
103     d->m_radiusSlider->setStyleName("CommonSliderInverted");
104     d->m_radiusSlider->setRange(0, 100);
105     d->m_radiusSlider->setValue(50);
106     MLocale locale;
107     d->m_radiusSlider->setHandleLabel(locale.formatPercent(0));
108     connect(d->m_radiusSlider, SIGNAL(valueChanged(int)),
109             SLOT(handleRadiusValueChanged(int)));
110     connect(d->m_radiusSlider, SIGNAL(sliderPressed()),
111             SLOT(showRadiusSliderHandleLabel()));
112     connect(d->m_radiusSlider, SIGNAL(sliderReleased()),
113             SLOT(hideRadiusSliderHandleLabel()));
114
115
116     MLayout* mainLayout = new MLayout();
117     mainLayout->setContentsMargins(0, 0, 0, 0);
118     mainLayout->setAnimation(0);
119
120     d->m_landscapePolicy = new MGridLayoutPolicy(mainLayout);
121     d->m_landscapePolicy->setContentsMargins(0, 0, 0, 0);
122     d->m_landscapePolicy->setSpacing(0);
123     d->m_landscapePolicy->addItem(d->m_enlargeButton,  0, 0);
124     d->m_landscapePolicy->addItem(d->m_shrinkButton,   0, 1);
125     d->m_landscapePolicy->addItem(d->m_radiusLabel,    0, 2);
126     d->m_landscapePolicy->addItem(d->m_radiusSlider,   0, 3);
127     d->m_landscapePolicy->addItem(d->m_aboutSeparator, 1, 0, 1, 4);
128     d->m_landscapePolicy->addItem(d->m_aboutWidget,    2, 0, 1, 4);
129     d->m_landscapePolicy->setAlignment(d->m_enlargeButton,  Qt::AlignVCenter | Qt::AlignHCenter);
130     d->m_landscapePolicy->setAlignment(d->m_shrinkButton,   Qt::AlignVCenter | Qt::AlignHCenter);
131     d->m_landscapePolicy->setAlignment(d->m_radiusLabel,    Qt::AlignVCenter | Qt::AlignHCenter);
132     d->m_landscapePolicy->setAlignment(d->m_radiusSlider,   Qt::AlignVCenter | Qt::AlignHCenter);
133     d->m_landscapePolicy->setAlignment(d->m_aboutSeparator, Qt::AlignVCenter | Qt::AlignHCenter);
134     d->m_landscapePolicy->setAlignment(d->m_aboutWidget,    Qt::AlignVCenter | Qt::AlignHCenter);
135     mainLayout->setLandscapePolicy(d->m_landscapePolicy);
136
137     d->m_portraitPolicy = new MGridLayoutPolicy(mainLayout);
138     d->m_portraitPolicy->setContentsMargins(0, 0, 0, 0);
139     d->m_portraitPolicy->setSpacing(0);
140     d->m_portraitPolicy->addItem(d->m_enlargeButton,  0, 0, 1, 2);
141     d->m_portraitPolicy->addItem(d->m_shrinkButton,   1, 0, 1, 2);
142     d->m_portraitPolicy->addItem(d->m_radiusLabel,    2, 0);
143     d->m_portraitPolicy->addItem(d->m_radiusSlider,   2, 1);
144     d->m_portraitPolicy->addItem(d->m_aboutSeparator, 3, 0, 1, 2);
145     d->m_portraitPolicy->addItem(d->m_aboutWidget,    4, 0, 1, 2);
146     d->m_portraitPolicy->setAlignment(d->m_enlargeButton,  Qt::AlignVCenter | Qt::AlignHCenter);
147     d->m_portraitPolicy->setAlignment(d->m_shrinkButton,   Qt::AlignVCenter | Qt::AlignHCenter);
148     d->m_portraitPolicy->setAlignment(d->m_radiusLabel,    Qt::AlignVCenter | Qt::AlignHCenter);
149     d->m_portraitPolicy->setAlignment(d->m_radiusSlider,   Qt::AlignVCenter | Qt::AlignHCenter);
150     d->m_portraitPolicy->setAlignment(d->m_aboutSeparator, Qt::AlignVCenter | Qt::AlignHCenter);
151     d->m_portraitPolicy->setAlignment(d->m_aboutWidget,    Qt::AlignVCenter | Qt::AlignHCenter);
152     mainLayout->setPortraitPolicy(d->m_portraitPolicy);
153
154     setLayout(mainLayout);
155
156     // Make sure initial policy is correct
157     if (MApplication::activeWindow()) {
158         if (MApplication::activeWindow()->orientation() == M::Portrait) {
159             d->m_portraitPolicy->activate();
160         } else {
161             d->m_landscapePolicy->activate();
162         }
163     } else {
164         d->m_landscapePolicy->activate();
165     }
166 }
167
168 GalleryEnlargeShrinkWidget::~GalleryEnlargeShrinkWidget()
169 {
170     delete d_ptr;
171     d_ptr = 0;
172 }
173
174 int GalleryEnlargeShrinkWidget::radius() const
175 {
176     Q_D(const GalleryEnlargeShrinkWidget);
177     return d->m_radiusSlider->value();
178 }
179
180 bool GalleryEnlargeShrinkWidget::shrink() const
181 {
182     Q_D(const GalleryEnlargeShrinkWidget);
183     return (d->m_effectButtonGroup->checkedId() == SHRINK_BUTTON_ID);
184
185 }
186
187 bool GalleryEnlargeShrinkWidget::isRadiusSliderHandleLabelVisible() const
188 {
189     Q_D(const GalleryEnlargeShrinkWidget);
190     return d->m_radiusSlider->isHandleLabelVisible();
191 }
192
193 void GalleryEnlargeShrinkWidget::showRadiusSliderHandleLabel()
194 {
195     Q_D(GalleryEnlargeShrinkWidget);
196     d->m_radiusSlider->setHandleLabelVisible(true);
197 }
198
199 void GalleryEnlargeShrinkWidget::hideRadiusSliderHandleLabel()
200 {
201     Q_D(GalleryEnlargeShrinkWidget);
202     d->m_radiusSlider->setHandleLabelVisible(false);
203 }
204
205 void GalleryEnlargeShrinkWidget::handleRadiusValueChanged(int newValue)
206 {
207     if (isRadiusSliderHandleLabelVisible()) {
208         MLocale locale;
209         QString value(locale.formatPercent(newValue/100.0f));
210         Q_D(GalleryEnlargeShrinkWidget);
211         d->m_radiusSlider->setHandleLabel(value);
212     }
213 }
214
215 void GalleryEnlargeShrinkWidget::enableInput(bool enabled)
216 {
217     // Don't disable the complete widget as we still want to react to
218     // device orientation changes and click in the About widget links.
219     Q_D(GalleryEnlargeShrinkWidget);
220
221     d->m_enlargeButton->setEnabled(enabled);
222     d->m_shrinkButton->setEnabled(enabled);
223     d->m_shrinkButton->setEnabled(enabled);
224     d->m_radiusSlider->setEnabled(enabled);
225     d->m_radiusLabel->setEnabled(enabled);
226 }