File: columnselectorwidget.cpp

package info (click to toggle)
zegrapher 3.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: cpp: 12,779; xml: 136; sh: 9; makefile: 4
file content (274 lines) | stat: -rw-r--r-- 8,676 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/****************************************************************************
**  Copyright (c) 2016, Adel Kara Slimane <adel.ks@zegrapher.com>
**
**  This file is part of ZeGrapher's source code.
**
**  ZeGrapher is free software: you may copy, redistribute and/or modify it
**  under the terms of the GNU General Public License as published by the
**  Free Software Foundation, either version 3 of the License, or (at your
**  option) any later version.
**
**  This file is distributed in the hope that it will be useful, but
**  WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/



#include "DataPlot/columnselectorwidget.h"

ColumnSelectorWidget::ColumnSelectorWidget(int count, int xindex, int yindex, int selectorindex)
{      
    setCoordinateSystem(CARTESIAN);

    columnCount = count;

    xselector.index = xindex;
    xselector.draw = true;
    xselector.betweenColumns = false;   

    yselector.index = yindex;
    yselector.draw = true;
    yselector.betweenColumns = false;  

    selector.index = selectorindex;
    selector.draw = true;
    selector.betweenColumns = false;
    selector.image.load(":icons/selector.png");


    draggedSelector = NULL;

    selectors << &xselector << &yselector << &selector;

    timer.setInterval(ANIMATION_PERIOD);
    connect(&timer, SIGNAL(timeout()), this, SLOT(updateAnimationProgress()));  
}

int ColumnSelectorWidget::getXindex()
{
    return xselector.index;
}

int ColumnSelectorWidget::getYindex()
{
    return yselector.index;
}

void ColumnSelectorWidget::updateSelectorsPos()
{
    xselector.pos = QPoint(xselector.index*width()/columnCount + (width()/columnCount - xselector.image.width())/2 , height() - xselector.image.height());
    yselector.pos = QPoint(yselector.index*width()/columnCount + (width()/columnCount - yselector.image.width())/2 , height() - yselector.image.height());
    selector.pos = QPoint(selector.index*width()/columnCount + (width()/columnCount - selector.image.width())/2 , height() - selector.image.height());
}

void ColumnSelectorWidget::askedForSelector()
{
    selector.draw = false;
    repaint();
}

void ColumnSelectorWidget::updateAnimationProgress()
{
    for(int i = 0 ; i < animatedSelectors.size(); i++)
    {
        animatedSelectors[i]->animation.progress += ANIMATION_PERIOD;
        animatedSelectors[i]->pos.setX(animatedSelectors[i]->animation.departureAbscissa + animatedSelectors[i]->animation.progress*(animatedSelectors[i]->animation.arrivalAbscissa - animatedSelectors[i]->animation.departureAbscissa)/ANIMATION_TIME);

        if(animatedSelectors[i]->animation.progress == ANIMATION_TIME)
            animatedSelectors.removeAt(i);
    }

    if(animatedSelectors.isEmpty())
        timer.stop();

    repaint();
}

void ColumnSelectorWidget::setCoordinateSystem(bool cartesian)
{
    coordinateSystem = cartesian;

    if(coordinateSystem == POLAR)
    {
        xselector.image.load(":/icons/Tetapin.png");
        yselector.image.load(":/icons/Rpin.png");
    }
    else if(coordinateSystem == CARTESIAN)
    {
        xselector.image.load(":/icons/Xpin.png");
        yselector.image.load(":/icons/Ypin.png");
    }

    repaint();
}

void ColumnSelectorWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    painter.begin(this);   

    drawSelectors();

    painter.end();
}

void ColumnSelectorWidget::drawSelectors()
{

    for(int i = 0 ; i < selectors.size(); i++)
    {
        if(selectors[i]->draw)
        {            
            painter.drawImage(selectors[i]->pos, selectors[i]->image);
        }
    }
}

void ColumnSelectorWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->x() >= width() - draggedSelector->image.width()/2)
    {
        draggedSelector->pos.setX(width() - draggedSelector->image.width());
    }
    else if(event->x() <= draggedSelector->image.width()/2)
    {
         draggedSelector->pos.setX(0);
    }
    else
    {
        draggedSelector->pos.setX(event->x() - draggedSelector->image.width()/2);       
    }
    repaint();
}

void ColumnSelectorWidget::mousePressEvent(QMouseEvent *event)
{
    bool found = false;
    int i = 0;
    QRect rect;

    selectors.push_front(&selector);// to checks first if the main selector is clicked

    while(i < selectors.size()-1 && !found)
    {
        rect = selectors[i]->image.rect();
        rect.translate(selectors[i]->pos);

        found = rect.contains(event->pos());
        if(found)
            draggedSelector = selectors[i];

        i++;
    }

    selectors.removeFirst();

    if(!found)
    {
        emit askForSelector();
        selector.draw = true;
        draggedSelector = &selector;
        mouseMoveEvent(event);
    }

    repaint();
}

Selector* ColumnSelectorWidget::otherSelector(Selector* notThisOne)
{
    if(notThisOne == &xselector)
        return &yselector;
    else return &xselector;
}

void ColumnSelectorWidget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    int x = draggedSelector->pos.x() + draggedSelector->image.width()/2;
    int index = x * columnCount / width();
    int abscissa = index * width() / columnCount + width()/columnCount/2;

    if(draggedSelector != &selector || (index == 0 && x <= 3*width()/columnCount/4) || (index == columnCount-1 && x >= width()-3*width()/columnCount/4) || abs(x - abscissa) <= width()/columnCount/4) //nearest to column
    {
        if(draggedSelector != &selector && otherSelector(draggedSelector)->index == index)
        {// the user putted the x pin over the y pin, or conversely, we move the putted on pin to dragged pin's old position
            Selector *sel = otherSelector(draggedSelector);

            sel->index = draggedSelector->index;
            sel->animation.progress = 0;
            sel->animation.departureAbscissa = sel->pos.x();
            sel->animation.arrivalAbscissa = sel->index * width() / columnCount + width()/columnCount/2 - sel->image.width()/2;
            animatedSelectors.removeOne(sel);
            animatedSelectors << sel;

            if(sel == &xselector)
                emit newXIndex(sel->index);
        }
        draggedSelector->index = index;
        draggedSelector->betweenColumns = false;
        draggedSelector->animation.progress = 0;
        draggedSelector->animation.departureAbscissa = draggedSelector->pos.x();
        draggedSelector->animation.arrivalAbscissa = abscissa - draggedSelector->image.width()/2;
        animatedSelectors.removeOne(draggedSelector);
        animatedSelectors << draggedSelector;
    }
    else // nearest to between two columns
    {
        if(x - abscissa < 0) // to index
        {
            draggedSelector->index = index;
            draggedSelector->betweenColumns = true;
            draggedSelector->animation.progress = 0;
            draggedSelector->animation.departureAbscissa = draggedSelector->pos.x();
            draggedSelector->animation.arrivalAbscissa = index * width() / columnCount - draggedSelector->image.width()/2;
            animatedSelectors.removeOne(draggedSelector);
            animatedSelectors << draggedSelector;
        }
        else // to index+1
        {
            draggedSelector->index = index+1;
            draggedSelector->betweenColumns = true;
            draggedSelector->animation.progress = 0;
            draggedSelector->animation.departureAbscissa = draggedSelector->pos.x();
            draggedSelector->animation.arrivalAbscissa = (index+1) * width() / columnCount - draggedSelector->image.width()/2;
            animatedSelectors.removeOne(draggedSelector);
            animatedSelectors << draggedSelector;
        }
    }

    if(draggedSelector == &selector)
        emit newSelectorPos(selector.betweenColumns, selector.index);
    else if(draggedSelector == &xselector)
        emit newXIndex(draggedSelector->index);
    else emit newYIndex(draggedSelector->index);

    timer.start();
}

void ColumnSelectorWidget::setColumnCount(int count)
{
    columnCount = count;

    if(xselector.index >= count)
    {
        if(yselector.index != count-1)
            xselector.index = count-1;
        else xselector.index = count-2;
    }
    else if(yselector.index >= count)
    {
        if(xselector.index != count-1)
            yselector.index = count-1;
        else yselector.index = count-2;
    }

    updateSelectorsPos();
}