optionsWidget.cc 13.4 KB
Newer Older
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
//
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
//
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
//  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: 1909 $
//   $Author: wilden $
//   $Date: 2008-06-03 18:45:21 +0200 (Tue, 03 Jun 2008) $
//
//=============================================================================




#include "optionsWidget.hh"
#include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh>
Dirk Wilden's avatar
Dirk Wilden committed
38 39
#include <ACG/Scenegraph/DrawModes.hh>
#include <OpenFlipper/ACGHelper/DrawModeConverter.hh>
40
#include <OpenFlipper/INIFile/INIFile.hh>
Dirk Wilden's avatar
Dirk Wilden committed
41 42

OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyBinding>& _core, QWidget *parent)
Jan Möbius's avatar
Jan Möbius committed
43 44 45 46
  : QWidget(parent),
    plugins_(_plugins),
    coreKeys_(_core)

47 48
{
  setupUi(this);
Dirk Wilden's avatar
Dirk Wilden committed
49

50 51
  connect(applyButton,SIGNAL(clicked()),this,SLOT(slotApply()));
  connect(cancelButton,SIGNAL(clicked()),this,SLOT(slotCancel()));
Jan Möbius's avatar
Jan Möbius committed
52 53
  connect(checkUpdateButton,SIGNAL(clicked()),this,SLOT(slotCheckUpdates()));

Dirk Wilden's avatar
Dirk Wilden committed
54 55 56
  uint mode = 2;
  for (uint i=1; i < 22; i++) {
    std::vector< QString > dm = drawModeToList( mode );
Jan Möbius's avatar
Jan Möbius committed
57

58
    if ( !dm.empty() && dm[0].trimmed() != ""){
Dirk Wilden's avatar
Dirk Wilden committed
59
      QListWidgetItem* item = new QListWidgetItem(dm[0]);
Jan Möbius's avatar
Jan Möbius committed
60

Dirk Wilden's avatar
Dirk Wilden committed
61 62 63 64 65
      item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
      item->setCheckState(Qt::Unchecked);

      availDrawModes->addItem( item );
    }
66

Dirk Wilden's avatar
Dirk Wilden committed
67 68
    mode = mode<<1;

69
  }
Jan Möbius's avatar
Jan Möbius committed
70

Jan Möbius's avatar
Jan Möbius committed
71
  http = new QHttp(this);
Jan Möbius's avatar
Jan Möbius committed
72

Jan Möbius's avatar
Jan Möbius committed
73 74 75 76 77 78 79
  // http specific connections
  connect(http, SIGNAL(requestFinished(int, bool)),
        this, SLOT(httpRequestFinished(int, bool)));
  connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
        this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
  connect(http, SIGNAL(dataReadProgress(int, int)),
          this, SLOT(updateDataReadProgress(int, int)));
Jan Möbius's avatar
Jan Möbius committed
80

Jan Möbius's avatar
Jan Möbius committed
81 82 83

  progressDialog = new QProgressDialog(this);
  connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
84 85 86
}

void OptionsWidget::showEvent ( QShowEvent * event ) {
Dirk Wilden's avatar
Dirk Wilden committed
87

Jan Möbius's avatar
Jan Möbius committed
88
  //general
Dirk Wilden's avatar
Dirk Wilden committed
89 90 91 92
  fullscreen->setChecked( OpenFlipper::Options::fullScreen() );
  splash->setChecked( OpenFlipper::Options::splash() );
  toolbox->setChecked( !OpenFlipper::Options::hideToolbox() );
  logger->setChecked( !OpenFlipper::Options::hideLogger() );
Dirk Wilden's avatar
Dirk Wilden committed
93
  enableLogFile->setChecked( OpenFlipper::Options::logFileEnabled() );
Jan Möbius's avatar
Jan Möbius committed
94

Dirk Wilden's avatar
Dirk Wilden committed
95 96
  //paths
  logFile->setText( OpenFlipper::Options::logFile() );
Dirk Wilden's avatar
Dirk Wilden committed
97 98 99 100 101 102

  //viewer
  backfaceCulling->setChecked( OpenFlipper::Options::backfaceCulling() );
  wZoomFactor->setText( QString::number(OpenFlipper::Options::wheelZoomFactor(), 'f') );
  wZoomFactorShift->setText( QString::number(OpenFlipper::Options::wheelZoomFactorShift(), 'f') );

103 104 105
  restrictFPS->setChecked( OpenFlipper::Options::restrictFrameRate() );
  FPS->setValue( OpenFlipper::Options::maxFrameRate() );

106 107 108 109 110
  // updates
  updateUser->setText( OpenFlipper::Options::updateUsername() );
  updatePass->setText( OpenFlipper::Options::updatePassword() );
  updateURL->setText( OpenFlipper::Options::updateUrl( ) );

Jan Möbius's avatar
Jan Möbius committed
111 112 113
  // debugging
  slotDebugging->setChecked(OpenFlipper::Options::doSlotDebugging());

114 115 116
  //set drawmodes


Dirk Wilden's avatar
Dirk Wilden committed
117
  //keyBindings
118 119
  keyTree->clear();

Dirk Wilden's avatar
Dirk Wilden committed
120 121 122 123 124 125 126 127 128 129 130 131 132
  keyTree->setColumnCount ( 2 );

  QStringList headerdata;
  headerdata << "Action" << "Shortcut";
  keyTree->setHeaderLabels(headerdata);

  //add Core Keys
  QTreeWidgetItem * core = new QTreeWidgetItem(keyTree, QStringList("CoreWidget"));

  QList<QTreeWidgetItem *> keys;

  for (uint i=0; i < coreKeys_.size(); i++){
    QStringList row;
Jan Möbius's avatar
Jan Möbius committed
133 134
    QKeySequence keySequence( coreKeys_[i].key + coreKeys_[i].modifiers );
    row << coreKeys_[i].description << keySequence.toString();
Dirk Wilden's avatar
Dirk Wilden committed
135 136 137 138 139 140 141 142 143 144
    keys.append(new QTreeWidgetItem(core, row));
  }

  core->addChildren(keys);
  keyTree->addTopLevelItem( core );



  QList<QTreeWidgetItem *> plugins;

Dirk Wilden's avatar
Dirk Wilden committed
145 146
  int off = 0;

Dirk Wilden's avatar
Dirk Wilden committed
147
  for (uint i=0; i < plugins_.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
148 149
//      if (( (plugins_[i]).keys).size() == 0)
//        continue;
150

Dirk Wilden's avatar
Dirk Wilden committed
151 152 153 154 155 156
    plugins.append(new QTreeWidgetItem(keyTree, QStringList( plugins_[i].name )));

    QList<QTreeWidgetItem *> keys;

    for (int k=0; k < plugins_[i].keys.count(); k++){
      QStringList row;
Jan Möbius's avatar
Jan Möbius committed
157 158
      QKeySequence keySequence( plugins_[i].keys[k].key + plugins_[i].keys[k].modifiers );
      row << plugins_[i].keys[k].description << keySequence.toString();
Dirk Wilden's avatar
Dirk Wilden committed
159
      keys.append(new QTreeWidgetItem(plugins[i-off], row));
Dirk Wilden's avatar
Dirk Wilden committed
160 161
    }

Dirk Wilden's avatar
Dirk Wilden committed
162 163 164 165 166 167 168
    if (keys.count() > 0)
      plugins[i-off]->addChildren(keys);
    else{
      delete plugins[i-off];
      plugins.removeLast();
      off++;
    }
Dirk Wilden's avatar
Dirk Wilden committed
169 170
  }

171 172
  if (plugins.count() > 0)
    keyTree->addTopLevelItems( plugins );
Dirk Wilden's avatar
Dirk Wilden committed
173

174
  keyTree->setColumnWidth(0,350);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189


  for (int i = 0 ; i < availDrawModes->count(); ++i )
    availDrawModes->item( i )->setCheckState(Qt::Unchecked) ;

  //Check the drawModes from StandardDrawMode
  std::vector< QString > dm = drawModeToList( OpenFlipper::Options::standardDrawMode() );

  for (uint i=0; i < dm.size(); i++){
    if ( !dm.empty() ) {
      QList< QListWidgetItem* > found = availDrawModes->findItems(dm[i],Qt::MatchExactly);
      for(int k=0; k < found.count(); k++)
        (found[k])->setCheckState(Qt::Checked);
    }
  }
190 191 192
}

void OptionsWidget::slotApply() {
Dirk Wilden's avatar
Dirk Wilden committed
193

Jan Möbius's avatar
Jan Möbius committed
194
  //general
Dirk Wilden's avatar
Dirk Wilden committed
195 196 197 198
  OpenFlipper::Options::fullScreen( fullscreen->isChecked() );
  OpenFlipper::Options::splash( splash->isChecked() );
  OpenFlipper::Options::hideToolbox( !toolbox->isChecked() );
  OpenFlipper::Options::hideLogger( !logger->isChecked() );
Dirk Wilden's avatar
Dirk Wilden committed
199 200 201 202
  OpenFlipper::Options::logFileEnabled( enableLogFile->isChecked() );

  //paths
  OpenFlipper::Options::logFile( logFile->text() );
Dirk Wilden's avatar
Dirk Wilden committed
203 204 205 206 207 208

  //viewer
  OpenFlipper::Options::backfaceCulling( backfaceCulling->isChecked() );
  OpenFlipper::Options::wheelZoomFactor( wZoomFactor->text().toDouble() );
  OpenFlipper::Options::wheelZoomFactorShift( wZoomFactorShift->text().toDouble() );

209 210 211
  OpenFlipper::Options::restrictFrameRate( restrictFPS->isChecked() );
  OpenFlipper::Options::maxFrameRate( FPS->value() );

212 213 214 215 216
  // updates
  OpenFlipper::Options::updateUrl( updateURL->text() );
  OpenFlipper::Options::updateUsername( updateUser->text() );
  OpenFlipper::Options::updatePassword( updatePass->text() );

Jan Möbius's avatar
Jan Möbius committed
217 218 219
  // Debugging
  OpenFlipper::Options::doSlotDebugging(slotDebugging->isChecked());

Dirk Wilden's avatar
Dirk Wilden committed
220 221 222 223 224 225 226
  //standardDrawMode
  std::vector< QString > mode;

  for (int i=0; i < availDrawModes->count(); i++)
    if (availDrawModes->item(i)->checkState() == Qt::Checked)
      mode.push_back( availDrawModes->item(i)->text() );

227
  OpenFlipper::Options::standardDrawMode( ListToDrawMode(mode) );
Dirk Wilden's avatar
Dirk Wilden committed
228

229
  emit applyOptions();
Dirk Wilden's avatar
Dirk Wilden committed
230
  emit saveOptions();
231

232 233 234 235 236 237
  hide();
}

void OptionsWidget::slotCancel() {
  hide();
}
Jan Möbius's avatar
Jan Möbius committed
238

Jan Möbius's avatar
Jan Möbius committed
239 240
void OptionsWidget::startDownload( QString _url ) {
   QUrl url(_url);
Jan Möbius's avatar
Jan Möbius committed
241

Jan Möbius's avatar
Jan Möbius committed
242 243
   // If username or passowrd are supplied, use them
   if ( ! updateUser->text().isEmpty() )
Jan Möbius's avatar
Jan Möbius committed
244 245 246 247 248
    url.setUserName(updateUser->text());

  if ( ! updatePass->text().isEmpty() )
    url.setPassword(updatePass->text());

Jan Möbius's avatar
Jan Möbius committed
249 250 251 252

  QFileInfo urlInfo(_url);

  // Download the file to the Home Directory
Jan Möbius's avatar
Jan Möbius committed
253
  QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
Jan Möbius's avatar
Jan Möbius committed
254 255
                      ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + urlInfo.fileName() );

Jan Möbius's avatar
Jan Möbius committed
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
  QString fileName = fileInfo.filePath();

  if (QFile::exists(fileName)) {
    QFile::remove(fileName);
  }

  file = new QFile(fileName);
  if (!file->open(QIODevice::WriteOnly)) {
    std::cerr << "Unable to Open local file for writing" << std::endl;
    delete file;
    file = 0;
  } else {
    QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());

    if (!url.userName().isEmpty())
        http->setUser(url.userName(), url.password());

    httpRequestAborted = false;
    QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
    if (path.isEmpty())
      path = "/";

    httpGetId = http->get(path, file);

Jan Möbius's avatar
Jan Möbius committed
281
    statusLabel->setText("Getting Versions file from " + _url);
Jan Möbius's avatar
Jan Möbius committed
282 283 284 285 286

    progressDialog->setWindowTitle(tr("HTTP"));
    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
    checkUpdateButton->setEnabled(false);
  }
Jan Möbius's avatar
Jan Möbius committed
287 288 289 290 291 292 293 294

}

void OptionsWidget::slotCheckUpdates() {

   // http://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/

  downloadType = VERSIONS_FILE;
295
  QString ServerMainURL = updateURL->text() + "Versions.ini";
Jan Möbius's avatar
Jan Möbius committed
296 297 298

  startDownload(ServerMainURL);

Jan Möbius's avatar
Jan Möbius committed
299 300 301 302 303
}

void OptionsWidget::slotGetUpdates() {
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
bool OptionsWidget::isNewer(QString _current, QString _latest) {

  QStringList latestVersionParts  = _latest.split('.');
  QStringList currentVersionParts = _current.split('.');

  bool newer = false;

  for ( int i = 0 ; i < latestVersionParts.size(); ++i ) {

    if ( i+1 > currentVersionParts.size() ) {
      // Versions are identical up to now. But latest version has additional version => updated
      newer = true;
      break;
    }

    bool ok = false;
    double latest  = latestVersionParts[i].toInt(&ok);
    double current = currentVersionParts[i].toInt(&ok);

    if ( !ok )
      std::cerr << "Error when parsing version strings!" << std::endl;

    if ( latest > current ) {
      newer = true;
      break;
    }
  }

  return newer;

}

void OptionsWidget::compareVersions() {

  QString fileName = QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
                     ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "Versions.ini" ;

  INIFile ini;

  if ( ! ini.connect(fileName,false) ) {
    std::cerr << "Failed to connect to Versions ini file" << std::endl;
    return;
  }

  statusLabel->setText("Checking for new versions");

  bool newerVersionsAvailable = false;

  QString updatedComponents = "Updates found for ";

  if ( OpenFlipper::Options::isWindows() ) {
    QString coreVersion;

    if ( ini.get_entry(coreVersion, "Core" , "VersionWindows" )) {

      if ( isNewer( OpenFlipper::Options::coreVersion(), coreVersion ) ) {
        std::cerr << "Newer Version found for Core!" << std::endl;
        std::cerr << "Latest Version is " << coreVersion.toStdString() << std::endl;
        std::cerr << "Current Version is " << OpenFlipper::Options::coreVersion().toStdString() << std::endl;

        newerVersionsAvailable = true;
        updatedComponents += "Core " + OpenFlipper::Options::coreVersion() + " -> " + coreVersion;
      }
    }

  } else {
    std::cerr << "Updates for linux not implemented yet" << std::endl;
  }

  if ( newerVersionsAvailable ) {
    statusLabel->setText(updatedComponents);
  }

  ini.disconnect();
}

Jan Möbius's avatar
Jan Möbius committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void OptionsWidget::httpRequestFinished(int requestId, bool error)
{
    if (requestId != httpGetId)
        return;
    if (httpRequestAborted) {
        if (file) {
            file->close();
            file->remove();
            delete file;
            file = 0;
        }

        progressDialog->hide();
        return;
    }

    if (requestId != httpGetId)
        return;

    progressDialog->hide();
    file->close();

    if (error) {
        file->remove();
Jan Möbius's avatar
Jan Möbius committed
404
        statusLabel->setText(tr("Download failed: %1.").arg(http->errorString()));
Jan Möbius's avatar
Jan Möbius committed
405 406 407 408 409
        QMessageBox::information(this, tr("HTTP"),
                                  tr("Download failed: %1.")
                                  .arg(http->errorString()));
    } else {
        QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
Jan Möbius's avatar
Jan Möbius committed
410
        statusLabel->setText(tr("Downloaded %1").arg(file->fileName() ));
Jan Möbius's avatar
Jan Möbius committed
411 412 413 414 415
    }

    checkUpdateButton->setEnabled(true);
    delete file;
    file = 0;
416 417 418 419 420

    if ( !error ) {
      if ( downloadType == VERSIONS_FILE )
        compareVersions();
    }
Jan Möbius's avatar
Jan Möbius committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

void OptionsWidget::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
  switch (responseHeader.statusCode()) {
  case 200:                   // Ok
  case 301:                   // Moved Permanently
  case 302:                   // Found
  case 303:                   // See Other
  case 307:                   // Temporary Redirect
    // these are not error conditions
    break;

  default:
    QMessageBox::information(this, tr("HTTP"),
                              tr("Download failed: %1.")
                              .arg(responseHeader.reasonPhrase()));
    statusLabel->setText("Download failed: " + responseHeader.reasonPhrase());
    httpRequestAborted = true;
    progressDialog->hide();
    http->abort();
  }
 }

void OptionsWidget::cancelDownload()
{
  statusLabel->setText(tr("download canceled."));
  httpRequestAborted = true;
  http->abort();
  checkUpdateButton->setEnabled(true);
}

void OptionsWidget::updateDataReadProgress(int bytesRead, int totalBytes)
{
  if (httpRequestAborted)
    return;

  progressDialog->setMaximum(totalBytes);
  progressDialog->setValue(bytesRead);
}



Jan Möbius's avatar
Jan Möbius committed
464 465