textureProperties.cc 9.74 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
Jan Möbius's avatar
Jan Möbius committed
15
//
Jan Möbius's avatar
 
Jan Möbius committed
16
17
18
19
//  OpenFlipper 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 Lesser General Public License for more details.
Jan Möbius's avatar
Jan Möbius committed
20
//
Jan Möbius's avatar
 
Jan Möbius committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




#include "textureProperties.hh"
36

37
#ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
38
39
40
41
42
  #include <qwt_plot_grid.h>
  #include <qwt_text.h>
  #include <qwt_plot.h>
#endif

43
44
#include <float.h>

Jan Möbius's avatar
 
Jan Möbius committed
45
#include <QtGui>
46
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
47
48
49
50
51

texturePropertiesWidget::texturePropertiesWidget(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
Jan Möbius's avatar
Jan Möbius committed
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    connect(buttonBox, SIGNAL( clicked(QAbstractButton*) ), this , SLOT ( slotButtonBoxClicked(QAbstractButton*) ) );
    connect(textureList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(textureChanged(QListWidgetItem*)) );
    connect(textureList, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(textureAboutToChange(QListWidgetItem*)) );

    //remember changes
    connect(repeatBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(clampBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(centerBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(absBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );
    connect(scaleBox, SIGNAL( clicked() ), this , SLOT ( slotPropertiesChanged() ) );

    connect(max_val, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );
    connect(clamp_min, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );
    connect(clamp_max, SIGNAL( valueChanged(double) ), this , SLOT ( slotPropertiesChanged(double) ) );

68
69
    connect(changeImageButton, SIGNAL( clicked() ), this, SLOT( slotChangeImage() ) );

70
71
72

    texData_ = 0;

73
    #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
74
      QGridLayout* layout = new QGridLayout( originalData);
75

Dirk Wilden's avatar
Dirk Wilden committed
76
      originalDataHistogram_ = new QwtPlot(0);
77

Dirk Wilden's avatar
Dirk Wilden committed
78
      layout->addWidget( originalDataHistogram_ , 0,0  );
79

Dirk Wilden's avatar
Dirk Wilden committed
80
81
82
83
      QwtText axis_title_x("value");
  //     axis_title_x.setFont(arial);
      QwtText axis_title_y("count");
  //     axis_title_y.setFont(arial);
84

Dirk Wilden's avatar
Dirk Wilden committed
85
86
87
      originalDataHistogram_->setAxisTitle(QwtPlot::xBottom, axis_title_x);
      originalDataHistogram_->setAxisTitle(QwtPlot::yLeft, axis_title_y);
      originalDataHistogram_->setCanvasBackground(Qt::white);
88

Dirk Wilden's avatar
Dirk Wilden committed
89
90
91
92
93
94
      QwtPlotGrid *grid2 = new QwtPlotGrid;
      grid2->enableYMin(true);
      grid2->enableXMin(true);
      grid2->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
      grid2->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
      grid2->attach(originalDataHistogram_);
95

Dirk Wilden's avatar
Dirk Wilden committed
96
97
98
99
100
101
102
103
      QwtText curve_title("Delay Spread Histogram");
  //     curve_title.setFont(arial);
      histogramCurve_.setTitle(curve_title);
      histogramCurve_.attach(originalDataHistogram_);

  //     connect(distanceCheckBox,SIGNAL(stateChanged ( int ) ) , this , SLOT (slotDistanceCheckbox(int)) );
  //     connect(normalCheckBox,SIGNAL(stateChanged ( int ) ) , this , SLOT (slotNormalCheckbox(int)) );
    #endif
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
void texturePropertiesWidget::show(TextureData* _texData, int _id, QString _name){

  texData_ = _texData;
  id_ = _id;

  textureList->clear();

  for (uint i=0; i < texData_->textures().size(); i++)
    textureList->addItem( texData_->textures()[i].name );

  if ( textureList->count() == 0){
    QMessageBox msgBox(this);
    msgBox.setText("Cannot show Properties. No Textures available!");
    msgBox.exec();
    return;
  }

  if (id_ == -1)
    textureLabel->setText("<B>Global Textures</B>");
  else
    textureLabel->setText("<B>Textures for object '" + _name + "'</B>");

  textureList->setCurrentItem( textureList->item(0) );
  textureChanged( textureList->item(0) );

  propChanged_ = false;

  QDialog::show();
}

void texturePropertiesWidget::textureAboutToChange(QListWidgetItem* _item){

  if (propChanged_){
    QMessageBox msgBox(this);
    msgBox.setText("The properties of the current texture have been changed.");
    msgBox.setInformativeText("Do you want to apply these changes?");
    msgBox.setStandardButtons(QMessageBox::Apply | QMessageBox::Discard );
    msgBox.setDefaultButton(QMessageBox::Apply);
    int ret = msgBox.exec();

    if (ret == QMessageBox::Apply){
      //just hit the applyButton ;)
      for (int i=0; i < buttonBox->buttons().count(); i++)
        if ( buttonBox->standardButton( buttonBox->buttons()[i] ) == QDialogButtonBox::Apply )
          slotButtonBoxClicked( buttonBox->buttons()[i] );

      textureList->setCurrentItem( _item );
      textureChanged( _item );

    } else {
      propChanged_ = false;

      textureList->setCurrentItem( _item );
      textureChanged( _item );
    }
  }
}

void texturePropertiesWidget::textureChanged(QListWidgetItem* _item){

  //open changes for the last texture so switch back
  if ( propChanged_ ){
    textureList->setCurrentRow( curRow_ );
    return;
  }

  if ( !texData_->textureExists( _item->text() ) )
    return; //should not happen

  textureName_ = _item->text();

  //update the dialog
  Texture& texture = texData_->texture(textureName_);

  repeatBox->setChecked(texture.parameters.repeat);
  clampBox->setChecked(texture.parameters.clamp);
  centerBox->setChecked(texture.parameters.center);
  absBox->setChecked(texture.parameters.abs);

  max_val->setValue( texture.parameters.max_val );
  clamp_min->setValue( texture.parameters.clamp_min );
  clamp_max->setValue( texture.parameters.clamp_max );

190
191
192
193
194
195
196
197
198
  // Show the texture Image
  imageLabel->setPixmap(QPixmap::fromImage(texture.textureImage));
  imageLabel->setScaledContents(true);

  if ( !texture.filename.startsWith("/") )
    fileLabel->setText( "File: " + texture.filename );
  else
    fileLabel->setText( "File: " + OpenFlipper::Options::textureDirStr() + QDir::separator() + texture.filename );

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  // update plot only when dimension is 1
  if ( texture.dimension == 1) {
    std::vector< double > x,y;

    x.push_back(100.0);
    y.push_back(100.0);

    x.push_back(200.0);
    y.push_back(150.0);

    x.push_back(300.0);
    y.push_back(10.0);

    setOriginalData(x,y);
  }

  propChanged_ = false;
  curRow_ = textureList->currentRow();
}


220
void texturePropertiesWidget::setOriginalData(std::vector< double > _x, std::vector< double > _y  ) {
221
  #ifdef WITH_QWT
Dirk Wilden's avatar
Dirk Wilden committed
222
223
224
225
    double minx = FLT_MIN;
    double maxx = FLT_MIN;
    double miny = FLT_MAX;
    double maxy = FLT_MIN;
226

Dirk Wilden's avatar
Dirk Wilden committed
227
228
229
230
231
232
233
234
235
    for ( uint i = 0 ; i < _x.size(); ++ i ) {
      minx = std::min(minx,_x[0]);
      maxx = std::max(maxx,_x[0]);
      miny = std::min(miny,_y[0]);
      maxy = std::max(maxy,_y[0]);
    }
    histogramCurve_.setData(&_x[0], &_y[0], _x.size());
    originalDataHistogram_->replot();
  #endif
Jan Möbius's avatar
 
Jan Möbius committed
236
}
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

void texturePropertiesWidget::slotChangeImage() {

  QString fileName = QFileDialog::getOpenFileName(0, tr("Open Image"),
                                                  OpenFlipper::Options::currentDirStr(),
                                                  tr("Images (*.png *.xpm *.jpg)"));

  imageLabel->setText("File: " + fileName );

  // TODO : Set and bind the new image

  std::cerr << "filename was : " << fileName.toStdString() << std::endl;

}

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
void texturePropertiesWidget::slotButtonBoxClicked(QAbstractButton* _button){

  QDialogButtonBox::StandardButton btn = buttonBox->standardButton(_button);

  if ( btn == QDialogButtonBox::Apply || btn == QDialogButtonBox::Ok){

    //applySettings
    bool changed = false;

    Texture& texture = texData_->texture(textureName_);

    if ( texture.parameters.repeat != repeatBox->isChecked() ){
      texture.parameters.repeat=repeatBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.clamp != clampBox->isChecked() ){
      texture.parameters.clamp=clampBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.center != centerBox->isChecked() ){
      texture.parameters.center=centerBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.abs != absBox->isChecked() ){
      texture.parameters.abs=absBox->isChecked();
      changed = true;
    }
    if ( texture.parameters.scale != scaleBox->isChecked() ){
      texture.parameters.scale=scaleBox->isChecked();
      changed = true;
    }

    if ( texture.parameters.max_val != max_val->value() ){
      texture.parameters.max_val = max_val->value();
      changed = true;
    }

    if ( texture.parameters.clamp_min != clamp_min->value() ){
      texture.parameters.clamp_min = clamp_min->value();
      changed = true;
    }

    if ( texture.parameters.clamp_max != clamp_max->value() ){
      texture.parameters.clamp_max = clamp_max->value();
      changed = true;
    }

    //inform the plugin about the update
    if (changed)
      emit applyProperties(texData_, textureName_, id_ );

    propChanged_ = false;
  }

  if ( btn == QDialogButtonBox::Apply )
    return;
  else
    hide();
}

void texturePropertiesWidget::slotPropertiesChanged(double /*_value*/){
  propChanged_ = true;
}