Interval_select.cpp 10.6 KB
Newer Older
1
/*
Mathieu Faverge's avatar
Mathieu Faverge committed
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
 ** This file is part of the ViTE project.
 **
 ** This software is governed by the CeCILL-A license under French law
 ** and abiding by the rules of distribution of free software. You can
 ** use, modify and/or redistribute the software under the terms of the
 ** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
 ** URL: "http://www.cecill.info".
 **
 ** As a counterpart to the access to the source code and rights to copy,
 ** modify and redistribute granted by the license, users are provided
 ** only with a limited warranty and the software's author, the holder of
 ** the economic rights, and the successive licensors have only limited
 ** liability.
 **
 ** In this respect, the user's attention is drawn to the risks associated
 ** with loading, using, modifying and/or developing or reproducing the
 ** software by the user in light of its specific status of free software,
 ** that may mean that it is complicated to manipulate, and that also
 ** therefore means that it is reserved for developers and experienced
 ** professionals having in-depth computer knowledge. Users are therefore
 ** encouraged to load and test the software's suitability as regards
 ** their requirements in conditions enabling the security of their
 ** systems and/or data to be ensured and, more generally, to use and
 ** operate it in the same conditions as regards security.
 **
 ** The fact that you are presently reading this means that you have had
 ** knowledge of the CeCILL-A license and that you accept its terms.
 **
 **
 ** ViTE developers are (for version 0.* to 1.0):
 **
 **        - COULOMB Kevin
 **        - FAVERGE Mathieu
 **        - JAZEIX Johnny
 **        - LAGRASSE Olivier
 **        - MARCOUEILLE Jule
 **        - NOISETTE Pascal
 **        - REDONDY Arthur
 **        - VUCHENER Clément
 **
 */
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>

/* -- */
#include "common/common.hpp"
#include "common/Info.hpp"
#include "common/Message.hpp"
#include "interface/resource.hpp"
#include "interface/Interface.hpp"

#include "core/Core.hpp"
#include "interface/Interface_graphic.hpp"
/* -- */
#include "trace/values/Values.hpp"
#include "trace/EntityValue.hpp"
#include "trace/EntityTypes.hpp"
#include "trace/Entitys.hpp"
#include "trace/tree/Interval.hpp"
#include "trace/Container.hpp"
/* -- */
#include <QKeyEvent>

#include "interface/Interval_select.hpp"

74
#undef max
75
#include <limits>
76

77 78
using namespace std;

Mathieu Faverge's avatar
Mathieu Faverge committed
79
Interval_select::Interval_select(Interface_graphic * console,QWidget *parent) : QDialog(parent), _trace(nullptr),_console(console) {
80
    setupUi(this);
Mathieu Faverge's avatar
Mathieu Faverge committed
81 82 83 84 85 86
    connect( minSpinBox, SIGNAL(valueChanged( double )), SLOT(minSpinBoxValueChanged( double )) );
    connect( maxSpinBox, SIGNAL(valueChanged( double )), SLOT(maxSpinBoxValueChanged( double )) );

    connect( interval_slider, SIGNAL(lowerValueChanged( int )), SLOT(minSliderValueChanged( int )) );
    connect( interval_slider, SIGNAL(upperValueChanged( int )), SLOT(maxSliderValueChanged( int )) );

Mathieu Faverge's avatar
Mathieu Faverge committed
87
    QMetaObject::connectSlotsByName(nullptr);
Mathieu Faverge's avatar
Mathieu Faverge committed
88 89 90

    _auto_refresh = auto_refresh_box->isChecked();
    _applied      = false;
91 92
}

Mathieu Faverge's avatar
Mathieu Faverge committed
93
Interval_select::~Interval_select() = default;
94 95

Trace* Interval_select::get_trace() {
Mathieu Faverge's avatar
Mathieu Faverge committed
96
    return _trace;
97 98 99 100 101
}

void Interval_select::set_trace(Trace *trace) {
    // Initialize _trace
    _trace = trace;
Mathieu Faverge's avatar
Mathieu Faverge committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    min_value->setText(QString().setNum (Info::Entity::x_min));
    max_value->setText(QString().setNum (Info::Entity::x_max));

    minSpinBox->setMinimum(Info::Entity::x_min);
    minSpinBox->setMaximum(Info::Entity::x_max);
    maxSpinBox->setMinimum(Info::Entity::x_min);
    maxSpinBox->setMaximum(Info::Entity::x_max);

    //the slider stays in int, we will translate its values
    /*interval_slider->setMinimum(0);
     interval_slider->setMaximum(99);*/

    double delta=Info::Entity::x_max - Info::Entity::x_min;
    double step= 0.001 * delta; //make steps of .1% for spinboxes
    minSpinBox->setSingleStep(step);
    maxSpinBox->setSingleStep(step);

    update_values();
121 122 123 124 125
}

//called if a zoom is triggered outside of the window, to update slider position and spinboxes with the real values
void Interval_select::update_values() {

Mathieu Faverge's avatar
Mathieu Faverge committed
126
    if ( _trace != nullptr )
Mathieu Faverge's avatar
Mathieu Faverge committed
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
    {
        Element_pos _min;
        Element_pos _max;

        //choose which value to load the actual position from (can change if we are loading from a splitted file set)
        if(Info::Splitter::preview == true){
            _min = Info::Entity::x_min;
            _max = Info::Entity::x_max;
        }
        else if(Info::Splitter::load_splitted == true) {
            Info::Render::_x_min = Info::Splitter::_x_min;
            Info::Render::_x_max = Info::Splitter::_x_max;
            auto_refresh_box->setChecked(false);//set the checkbox to false because reload is expensive for splitted mode
            _min = Info::Splitter::_x_min;
            _max = Info::Splitter::_x_max;
        }
        else {
            _min = Info::Render::_x_min;
            _max = Info::Render::_x_max;
        }
        bool v  = minSpinBox->blockSignals(true);
        bool v2 = maxSpinBox->blockSignals(true);
        bool v3 = interval_slider->blockSignals(true);
        int maxvalue=int(_max*(interval_slider->maximum() - interval_slider->minimum())/(_trace->get_max_date() - minSpinBox->minimum()));
        interval_slider->setUpperValue(maxvalue);
        interval_slider->setLowerValue(int(_min*(interval_slider->maximum() - interval_slider->minimum())/(_trace->get_max_date() - minSpinBox->minimum())));
        maxSpinBox->setValue(_max);
        minSpinBox->setValue(_min);
        minSpinBox->blockSignals(v);
        maxSpinBox->blockSignals(v2);
        interval_slider->blockSignals(v3);
        _applied=false;
159 160 161 162
    }
}


Mathieu Faverge's avatar
Mathieu Faverge committed
163
void Interval_select::minSpinBoxValueChanged( double value )
164
{
Mathieu Faverge's avatar
Mathieu Faverge committed
165
    QPalette myPalette(minSpinBox->palette());
166

Mathieu Faverge's avatar
Mathieu Faverge committed
167
    if(value < maxSpinBox->value()) {
168

Mathieu Faverge's avatar
Mathieu Faverge committed
169 170 171 172
        bool v = interval_slider->blockSignals(true);
        double position = (interval_slider->maximum() - interval_slider->minimum())
            /             (minSpinBox->maximum()      - minSpinBox->minimum());
        position *= value;
173

Mathieu Faverge's avatar
Mathieu Faverge committed
174 175 176
        interval_slider->setLowerValue( int(position) );
        interval_slider->blockSignals(v);

177
        emit minValueChanged( minSpinBox->value() );
Mathieu Faverge's avatar
Mathieu Faverge committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

        myPalette.setColor(QPalette::Active, QPalette::Text,            Qt::black);
        myPalette.setColor(QPalette::Active, QPalette::HighlightedText, Qt::white);

        if(_auto_refresh)
            apply_settings();
    }
    else {
        //min cannot be greater than max
        myPalette.setColor(QPalette::Active, QPalette::Text,            Qt::red);
        myPalette.setColor(QPalette::Active, QPalette::HighlightedText, Qt::red);
        //minSliderValueChanged(  maxSpinBox->value() );
    }

    minSpinBox->setPalette(myPalette);
    maxSpinBox->setPalette(myPalette);

    _applied = false;
196 197
}

Mathieu Faverge's avatar
Mathieu Faverge committed
198
void Interval_select::maxSpinBoxValueChanged( double value )
199
{
Mathieu Faverge's avatar
Mathieu Faverge committed
200
    QPalette myPalette(minSpinBox->palette());
201

Mathieu Faverge's avatar
Mathieu Faverge committed
202 203 204 205 206 207 208 209 210 211
    if( value > minSpinBox->value() ) {

        bool v = interval_slider->blockSignals(true);
        double position = (interval_slider->maximum() - interval_slider->minimum())
            /             (maxSpinBox->maximum()      - maxSpinBox->minimum());
        position *= value;

        interval_slider->setLowerValue( int(position) );
        interval_slider->blockSignals(v);

212
        emit maxValueChanged( maxSpinBox->value() );
Mathieu Faverge's avatar
Mathieu Faverge committed
213 214 215 216 217 218

        myPalette.setColor(QPalette::Active, QPalette::Text,            Qt::black);
        myPalette.setColor(QPalette::Active, QPalette::HighlightedText, Qt::white);

        if(_auto_refresh)
            apply_settings();
219 220

    }
Mathieu Faverge's avatar
Mathieu Faverge committed
221 222 223 224 225 226
    else {
        //max cannot be lower than min
        myPalette.setColor(QPalette::Active, QPalette::Text, Qt::red);
        myPalette.setColor(QPalette::Active, QPalette::HighlightedText, Qt::red);
        //maxSliderValueChanged(  minSpinBox->value() ) ;
    }
227

Mathieu Faverge's avatar
Mathieu Faverge committed
228 229
    maxSpinBox->setPalette(myPalette);
    minSpinBox->setPalette(myPalette);
230

Mathieu Faverge's avatar
Mathieu Faverge committed
231
    _applied = false;
232 233
}

Mathieu Faverge's avatar
Mathieu Faverge committed
234 235 236 237 238 239
void Interval_select::minSliderValueChanged( int _value )
{
    if(_value < interval_slider->upperValue()){
        bool v = minSpinBox->blockSignals(true);
        minSpinBox->setValue( _value *(_trace->get_max_date() - minSpinBox->minimum())/(interval_slider->maximum() - interval_slider->minimum()));
        minSpinBox->blockSignals(v);
240
        emit minValueChanged( minSpinBox->value() );
Mathieu Faverge's avatar
Mathieu Faverge committed
241 242 243
        if(_auto_refresh)apply_settings();
    }
    _applied = false;
244 245
}

Mathieu Faverge's avatar
Mathieu Faverge committed
246 247 248 249 250 251 252
void Interval_select::maxSliderValueChanged( int _value )
{

    if(_value > interval_slider->lowerValue()){
        bool v = maxSpinBox->blockSignals(true);
        maxSpinBox->setValue( _value * (_trace->get_max_date() - maxSpinBox->minimum())/(interval_slider->maximum() - interval_slider->minimum()));
        maxSpinBox->blockSignals(v);
253
        emit maxValueChanged( maxSpinBox->value() );
Mathieu Faverge's avatar
Mathieu Faverge committed
254 255 256
        if(_auto_refresh)apply_settings();
    }
    _applied = false;
257

Mathieu Faverge's avatar
Mathieu Faverge committed
258
    std::cerr << "maxvalue changed" << std::endl;
259 260 261
}


Mathieu Faverge's avatar
Mathieu Faverge committed
262 263 264
void Interval_select::apply_settings(){
    if (! _applied) {
        if(minSpinBox->value()!=maxSpinBox->value()){
265 266 267
            Element_pos min = minSpinBox->value();
            Element_pos max = maxSpinBox->value();
            Element_pos zoom[2] = { min, max };
Mathieu Faverge's avatar
Mathieu Faverge committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

            //Info::Render::_x_min_visible = 0.0;
            //Info::Render::_x_max_visible = 0.0;
            _console->get_console()->launch_action(Core:: _STATE_CLEAN_RENDER_AREA);

            //reload data from disk if needed
#if defined(USE_ITC) && defined(BOOST_SERIALIZE)
            Info::Splitter::_x_min=zoom[0];
            Info::Splitter::_x_max=zoom[1];
            _trace->updateTrace(new Interval(zoom[0], zoom[1]));
#endif

            _console->get_console()->draw_trace(_console->get_filename(),Core::_DRAW_OPENGL);

            _console->get_console()->launch_action(Core:: _STATE_ZOOM_IN_AN_INTERVAL, &zoom);
            _console->get_console()->launch_action(Core:: _STATE_RENDER_UPDATE);
        }
    }
    _applied = true;
287 288 289
}


Mathieu Faverge's avatar
Mathieu Faverge committed
290 291 292 293 294
/*
 * Apply changes
 */
void Interval_select::on_apply_button_clicked(){
    apply_settings();
295 296 297
}

/*
Mathieu Faverge's avatar
Mathieu Faverge committed
298 299 300 301 302
 * Forgot about changes
 */
void Interval_select::on_cancel_button_clicked(){
    //reset()
    hide();
303 304
}

Mathieu Faverge's avatar
Mathieu Faverge committed
305 306 307 308 309 310
/*
 * Apply changes and quit
 */
void Interval_select::on_ok_button_clicked(){
    apply_settings();
    hide();
311 312
}

Mathieu Faverge's avatar
Mathieu Faverge committed
313 314 315 316 317 318
/*
 * Restore min/max values
 */
void Interval_select::on_reset_button_clicked() {
    interval_slider->setUpperValue( interval_slider->maximum() );
    interval_slider->setLowerValue( interval_slider->minimum() );
319 320
}

Mathieu Faverge's avatar
Mathieu Faverge committed
321 322 323 324 325
/*
 * Change status of auto-refresh
 */
void Interval_select::on_auto_refresh_box_stateChanged(){
    _auto_refresh = auto_refresh_box->isChecked();
326
}