1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef BACKGROUNDWIDGET_H
18 #define BACKGROUNDWIDGET_H
19 
20 #include <QWidget>
21 
22 #include "../Color.h"
23 #include "BackgroundData.h"
24 
25 class Background;
26 class ColorSelector;
27 
28 class QLineEdit;
29 class QPushButton;
30 class QCheckBox;
31 class QDoubleSpinBox;
32 class QComboBox;
33 
34 /// \class BackgroundWidget
35 /// BackgroundWidget is a widget that allows users to change
36 /// the value of a Background object in a graphical way
37 ///
38 /// Usage:
39 /// \code
40 /// Background * background = new Background();
41 /// BackgroundWidget * backgroundWidget = new BackgroundWidget();
42 /// backgroundWidget->setBackground(background);
43 /// \endcode
44 
45 class BackgroundWidget: public QWidget
46 {
47     Q_OBJECT
48 
49 public:
50     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
51 
52     // Constructor
53     BackgroundWidget(QWidget * parent = 0);
54 
55     // Set/Get which Background object is edited by the BackgroundWidget
56     void setBackground(Background * background);
57     Background * background() const;
58 
59 private slots:
60     // Update values from background
61     void updateFromBackground_();
62     void onBackgroundDestroyed_();
63 
64     // Process user interaction with widgets
65     // Color
66     void processColorSelectorColorChanged_(const Color & newColor);
67     // Image
68     void processImageLineEditEditingFinished_();
69     void processImageBrowseButtonClicked_();
70     void processImageRefreshButtonClicked_();
71     // Position
72     void processLeftSpinBoxValueChanged_(double newLeft);
73     void processTopSpinBoxValueChanged_(double newTop);
74     void processLeftSpinBoxEditingFinished_();
75     void processTopSpinBoxEditingFinished_();
76     // Size
77     void processSizeComboBoxCurrentIndexChanged_(int newSizeType);
78     void processWidthSpinBoxValueChanged_(double newWidth);
79     void processHeightSpinBoxValueChanged_(double newHeight);
80     void processWidthSpinBoxEditingFinished_();
81     void processHeightSpinBoxEditingFinished_();
82     // Repeat
83     void processRepeatComboBoxCurrentIndexChanged_(int newRepeatType);
84     // Opacity
85     void processOpacitySpinBoxValueChanged_(double newOpacity);
86     void processOpacitySpinBoxEditingFinished_();
87     // Hold
88     void processHoldCheckBoxToggled_(bool newHold);
89 
90 private:
91     // Background operated by BackgroundWidget
92     Background * background_;
93 
94     // GUI
95     // Color
96     ColorSelector * colorSelector_;
97     // Images
98     QLineEdit * imageLineEdit_;
99     QPushButton * imageBrowseButton_;
100     QPushButton * imageRefreshButton_;
101     // Position
102     QDoubleSpinBox * leftSpinBox_;
103     QDoubleSpinBox * topSpinBox_;
104     // Size
105     QComboBox * sizeComboBox_;
106     QDoubleSpinBox * widthSpinBox_;
107     QDoubleSpinBox * heightSpinBox_;
108     // Repeat
109     QComboBox * repeatComboBox_;
110     // Opacity
111     QDoubleSpinBox * opacitySpinBox_;
112     // Hold
113     QCheckBox * holdCheckBox_;
114 
115     // Guard needed for updateFromBackground_()
116     // It is needed is to avoid modifying back 'this->background_' when
117     // 'this' updates its widget values from 'this->background_'
118     bool isUpdatingFromBackground_;
119 
120     // Mechanism for issuing undo commands
121     // We keep a local copy of background data to compare values before and after
122     // editing, in order to issue an undoable command only when they differ
123     void emitCheckpoint_();
124     bool isBeingEdited_;
125     BackgroundData dataBeforeEditing_;
126 };
127 
128 #endif // BACKGROUNDWIDGET_H
129