expert.cpp 15.4 KB
Newer Older
dimitri's avatar
dimitri committed
1 2 3 4
#include "expert.h"
#include "inputbool.h"
#include "inputstring.h"
#include "inputint.h"
dimitri's avatar
dimitri committed
5 6 7 8
#include "inputstring.h"
#include "inputstrlist.h"
#include <QtGui>
#include <QtXml>
dimitri's avatar
dimitri committed
9 10 11
#include "config.h"
#include "version.h"

dimitri's avatar
dimitri committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#undef  SA
#define SA(x) QString::fromAscii(x)

static QString convertToComment(const QString &s)
{
  if (s.isEmpty()) 
  {
    return QString();
  }
  else
  {
    return SA("# ")+
           s.trimmed().replace(SA("\n"),SA("\n# "))+
           SA("\n");
  }
}

//------------------------------------------------------------------------------------
dimitri's avatar
dimitri committed
30

dimitri's avatar
dimitri committed
31
Expert::Expert()
dimitri's avatar
dimitri committed
32
{
dimitri's avatar
dimitri committed
33 34 35
  m_treeWidget = new QTreeWidget;
  m_treeWidget->setColumnCount(1);
  m_topicStack = new QStackedWidget;
dimitri's avatar
dimitri committed
36
  m_inShowHelp = FALSE;
dimitri's avatar
dimitri committed
37

dimitri's avatar
dimitri committed
38 39 40 41 42
  QFile file(SA(":/config.xml"));
  QString err;
  int errLine,errCol;
  QDomDocument configXml;
  if (file.open(QIODevice::ReadOnly))
dimitri's avatar
dimitri committed
43
  {
dimitri's avatar
dimitri committed
44
    if (!configXml.setContent(&file,false,&err,&errLine,&errCol))
dimitri's avatar
dimitri committed
45
    {
dimitri's avatar
dimitri committed
46 47 48 49 50
      QString msg = tr("Error parsing internal config.xml at line %1 column %2.\n%3").
                  arg(errLine).arg(errCol).arg(err);
      QMessageBox::warning(this, tr("Error"), msg);
      exit(1);
    }
dimitri's avatar
dimitri committed
51
  }
dimitri's avatar
dimitri committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  m_rootElement = configXml.documentElement();

  createTopics(m_rootElement);
  m_helper = new QTextEdit;
  m_helper->setReadOnly(true);
  m_splitter = new QSplitter(Qt::Vertical);
  m_splitter->addWidget(m_treeWidget);
  m_splitter->addWidget(m_helper);

  QWidget *rightSide = new QWidget;
  QGridLayout *grid = new QGridLayout(rightSide);
  m_prev = new QPushButton(tr("Previous"));
  m_prev->setEnabled(false);
  m_next = new QPushButton(tr("Next"));
  grid->addWidget(m_topicStack,0,0,1,2);
  grid->addWidget(m_prev,1,0,Qt::AlignLeft);
  grid->addWidget(m_next,1,1,Qt::AlignRight);
  grid->setColumnStretch(0,1);
  grid->setRowStretch(0,1);

  addWidget(m_splitter);
  addWidget(rightSide);
  connect(m_next,SIGNAL(clicked()),SLOT(nextTopic()));
dimitri's avatar
dimitri committed
75

dimitri's avatar
dimitri committed
76 77 78 79 80 81 82
  connect(m_prev,SIGNAL(clicked()),SLOT(prevTopic()));
}

Expert::~Expert()
{
  QHashIterator<QString,Input*> i(m_options);
  while (i.hasNext()) 
dimitri's avatar
dimitri committed
83
  {
dimitri's avatar
dimitri committed
84 85
    i.next();
    delete i.value();
dimitri's avatar
dimitri committed
86
  }
dimitri's avatar
dimitri committed
87
}
dimitri's avatar
dimitri committed
88

dimitri's avatar
dimitri committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
void Expert::createTopics(const QDomElement &rootElem)
{
  QList<QTreeWidgetItem*> items;
  QDomElement childElem = rootElem.firstChildElement();
  while (!childElem.isNull())
  {
    if (childElem.tagName()==SA("group"))
    {
      QString name = childElem.attribute(SA("name"));
      items.append(new QTreeWidgetItem((QTreeWidget*)0,QStringList(name)));
      QWidget *widget = createTopicWidget(childElem);
      m_topics[name] = widget;
      m_topicStack->addWidget(widget);
    }
    childElem = childElem.nextSiblingElement();
  }
  m_treeWidget->setHeaderLabels(QStringList() << SA("Topics"));
  m_treeWidget->insertTopLevelItems(0,items);
  connect(m_treeWidget,
          SIGNAL(currentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)),
          this,
          SLOT(activateTopic(QTreeWidgetItem *,QTreeWidgetItem *)));
dimitri's avatar
dimitri committed
111 112
}

dimitri's avatar
dimitri committed
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

QWidget *Expert::createTopicWidget(QDomElement &elem)
{
  QScrollArea *area   = new QScrollArea;
  QWidget     *topic  = new QWidget;
  QGridLayout *layout = new QGridLayout(topic);
  QDomElement child   = elem.firstChildElement();
  int row=0;
  while (!child.isNull())
  {
    QString type = child.attribute(SA("type"));
    if (type==SA("bool"))
    {
      InputBool *boolOption = 
          new InputBool(
            layout,row,
            child.attribute(SA("id")),
            child.attribute(SA("defval"))==SA("1"),
            child.attribute(SA("docs"))
           );
      m_options.insert(
          child.attribute(SA("id")),
          boolOption
         );
      connect(boolOption,SIGNAL(showHelp(Input*)),SLOT(showHelp(Input*)));
      connect(boolOption,SIGNAL(changed()),SIGNAL(changed()));
    }
    else if (type==SA("string"))
    {
      InputString::StringMode mode;
      QString format = child.attribute(SA("format"));
      if (format==SA("dir"))
      {
        mode = InputString::StringDir;
      }
      else if (format==SA("file"))
      {
        mode = InputString::StringFile;
      }
      else // format=="string"
      {
        mode = InputString::StringFree;
      }
      InputString *stringOption = 
          new InputString(
            layout,row,
            child.attribute(SA("id")),
            child.attribute(SA("defval")),
            mode,
dimitri's avatar
dimitri committed
162 163
            child.attribute(SA("docs")),
            child.attribute(SA("abspath"))
dimitri's avatar
dimitri committed
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 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 316 317 318 319 320 321 322 323 324 325 326
           );
      m_options.insert(
          child.attribute(SA("id")),
          stringOption
         );
      connect(stringOption,SIGNAL(showHelp(Input*)),SLOT(showHelp(Input*)));
      connect(stringOption,SIGNAL(changed()),SIGNAL(changed()));
    }
    else if (type==SA("enum"))
    {
      InputString *enumList = new InputString(
            layout,row,
            child.attribute(SA("id")),
            child.attribute(SA("defval")),
            InputString::StringFixed,
            child.attribute(SA("docs"))
           );
      QDomElement enumVal = child.firstChildElement();
      while (!enumVal.isNull())
      {
        enumList->addValue(enumVal.attribute(SA("name")));
        enumVal = enumVal.nextSiblingElement();
      }
      enumList->setDefault();

      m_options.insert(child.attribute(SA("id")),enumList);
      connect(enumList,SIGNAL(showHelp(Input*)),SLOT(showHelp(Input*)));
      connect(enumList,SIGNAL(changed()),SIGNAL(changed()));
    }
    else if (type==SA("int"))
    {
      InputInt *intOption = 
          new InputInt(
            layout,row,
            child.attribute(SA("id")),
            child.attribute(SA("defval")).toInt(),
            child.attribute(SA("minval")).toInt(),
            child.attribute(SA("maxval")).toInt(),
            child.attribute(SA("docs"))
          );
      m_options.insert(
          child.attribute(SA("id")),
          intOption
        );
      connect(intOption,SIGNAL(showHelp(Input*)),SLOT(showHelp(Input*)));
      connect(intOption,SIGNAL(changed()),SIGNAL(changed()));
    }
    else if (type==SA("list"))
    {
      InputStrList::ListMode mode;
      QString format = child.attribute(SA("format"));
      if (format==SA("dir"))
      {
        mode = InputStrList::ListDir;
      }
      else if (format==SA("file"))
      {
        mode = InputStrList::ListFile;
      }
      else if (format==SA("filedir"))
      {
        mode = InputStrList::ListFileDir;
      }
      else // format=="string"
      {
        mode = InputStrList::ListString;
      }
      QStringList sl;
      QDomElement listVal = child.firstChildElement();
      while (!listVal.isNull())
      {
        sl.append(listVal.attribute(SA("name")));
        listVal = listVal.nextSiblingElement();
      }
      InputStrList *listOption = 
          new InputStrList(
            layout,row,
            child.attribute(SA("id")),
            sl,
            mode,
            child.attribute(SA("docs"))
          );
      m_options.insert(
          child.attribute(SA("id")),
          listOption
        );
      connect(listOption,SIGNAL(showHelp(Input*)),SLOT(showHelp(Input*)));
      connect(listOption,SIGNAL(changed()),SIGNAL(changed()));
    }
    else if (type==SA("obsolete"))
    {
      // ignore
    }
    else // should not happen
    {
      printf("Unsupported type %s\n",qPrintable(child.attribute(SA("type"))));
    }
    child = child.nextSiblingElement();
  }

  // compute dependencies between options
  child = elem.firstChildElement();
  while (!child.isNull())
  {
    QString dependsOn = child.attribute(SA("depends"));
    QString id        = child.attribute(SA("id"));
    if (!dependsOn.isEmpty())
    {
       Input *parentOption = m_options[dependsOn];
       Input *thisOption   = m_options[id];
       Q_ASSERT(parentOption);
       Q_ASSERT(thisOption);
       if (parentOption && thisOption)
       {
         //printf("Adding dependency '%s' (%p)->'%s' (%p)\n",
         //  qPrintable(dependsOn),parentOption,
         //  qPrintable(id),thisOption);
         parentOption->addDependency(thisOption);
       }
    }
    child = child.nextSiblingElement();
  }

  // set initial dependencies
  QHashIterator<QString,Input*> i(m_options);
  while (i.hasNext()) 
  {
    i.next();
    if (i.value())
    {
      i.value()->updateDependencies();
    }
  }

  layout->setRowStretch(row,1);
  layout->setColumnStretch(1,2);
  layout->setSpacing(5);
  topic->setLayout(layout);
  area->setWidget(topic);
  area->setWidgetResizable(true);
  return area;
}

void Expert::activateTopic(QTreeWidgetItem *item,QTreeWidgetItem *)
{
  if (item)
  {
    QWidget *w = m_topics[item->text(0)];
    m_topicStack->setCurrentWidget(w);
    m_prev->setEnabled(m_topicStack->currentIndex()!=0); 
    m_next->setEnabled(m_topicStack->currentIndex()!=m_topicStack->count()-1); 
  }
}

void Expert::loadSettings(QSettings *s)
{
  QHashIterator<QString,Input*> i(m_options);
  while (i.hasNext()) 
  {
    i.next();
    QVariant var = s->value(SA("config/")+i.key());
    if (i.value())
    {
dimitri's avatar
dimitri committed
327
      //printf("Loading key %s: type=%d value='%s'\n",qPrintable(i.key()),var.type(),qPrintable(var.toString()));
dimitri's avatar
dimitri committed
328 329 330 331 332 333 334
      i.value()->value() = var;
      i.value()->update();
    }
  }
}

void Expert::saveSettings(QSettings *s)
dimitri's avatar
dimitri committed
335
{
dimitri's avatar
dimitri committed
336 337 338 339
  QHashIterator<QString,Input*> i(m_options);
  while (i.hasNext()) 
  {
    i.next();
dimitri's avatar
dimitri committed
340
    //printf("Saving key %s: type=%d value='%s'\n",qPrintable(i.key()),i.value()->value().type(),qPrintable(i.value()->value().toString()));
dimitri's avatar
dimitri committed
341 342
    if (i.value())
    {
dimitri's avatar
dimitri committed
343
      s->setValue(SA("config/")+i.key(),i.value()->value());
dimitri's avatar
dimitri committed
344 345
    }
  }
dimitri's avatar
dimitri committed
346 347
}

dimitri's avatar
dimitri committed
348
void Expert::loadConfig(const QString &fileName)
dimitri's avatar
dimitri committed
349
{
dimitri's avatar
dimitri committed
350 351
  //printf("Expert::loadConfig(%s)\n",qPrintable(fileName));
  parseConfig(fileName,m_options);
dimitri's avatar
dimitri committed
352 353
}

dimitri's avatar
dimitri committed
354 355
void Expert::saveTopic(QTextStream &t,QDomElement &elem,QTextCodec *codec,
                       bool brief)
dimitri's avatar
dimitri committed
356
{
dimitri's avatar
dimitri committed
357 358 359 360 361 362 363 364 365
  // write group header
  t << endl;
  t << "#---------------------------------------------------------------------------" << endl;
  t << "# " << elem.attribute(SA("docs")) << endl;
  t << "#---------------------------------------------------------------------------" << endl;

  // write options...
  QDomElement childElem = elem.firstChildElement();
  while (!childElem.isNull())
dimitri's avatar
dimitri committed
366
  {
dimitri's avatar
dimitri committed
367 368 369 370
    QString type = childElem.attribute(SA("type"));
    QString name = childElem.attribute(SA("id"));
    QHash<QString,Input*>::const_iterator i = m_options.find(name);
    if (i!=m_options.end())
dimitri's avatar
dimitri committed
371
    {
dimitri's avatar
dimitri committed
372 373 374 375 376 377 378 379 380 381 382 383 384
      Input *option = i.value();
      if (!brief)
      {
        t << endl;
        t << convertToComment(childElem.attribute(SA("docs")));
        t << endl;
      }
      t << name.leftJustified(23) << "= ";
      if (option)
      {
        option->writeValue(t,codec);
      }
      t << endl;
dimitri's avatar
dimitri committed
385
    }
dimitri's avatar
dimitri committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    childElem = childElem.nextSiblingElement();
  }

}

bool Expert::writeConfig(QTextStream &t,bool brief)
{
  if (!brief)
  {
    // write global header
    t << "# Doxyfile " << versionString << endl << endl; // TODO: add version
    t << "# This file describes the settings to be used by the documentation system\n";
    t << "# doxygen (www.doxygen.org) for a project\n";
    t << "#\n";
    t << "# All text after a hash (#) is considered a comment and will be ignored\n";
    t << "# The format is:\n";
    t << "#       TAG = value [value, ...]\n";
    t << "# For lists items can also be appended using:\n";
    t << "#       TAG += value [value, ...]\n";
    t << "# Values that contain spaces should be placed between quotes (\" \")\n";
  }

  QTextCodec *codec = 0;
  Input *option = m_options[QString::fromAscii("DOXYFILE_ENCODING")];
  if (option)
  {
    codec = QTextCodec::codecForName(option->value().toString().toAscii());
    if (codec==0) // fallback: use UTF-8
dimitri's avatar
dimitri committed
414
    {
dimitri's avatar
dimitri committed
415
      codec = QTextCodec::codecForName("UTF-8");
dimitri's avatar
dimitri committed
416 417
    }
  }
dimitri's avatar
dimitri committed
418 419 420 421 422 423 424
  QDomElement childElem = m_rootElement.firstChildElement();
  while (!childElem.isNull())
  {
    saveTopic(t,childElem,codec,brief);
    childElem = childElem.nextSiblingElement();
  }
  return true;
dimitri's avatar
dimitri committed
425 426
}

dimitri's avatar
dimitri committed
427
QByteArray Expert::saveInnerState () const
dimitri's avatar
dimitri committed
428
{
dimitri's avatar
dimitri committed
429 430 431 432 433 434 435 436 437 438
  return m_splitter->saveState();
}

bool Expert::restoreInnerState ( const QByteArray & state )
{
  return m_splitter->restoreState(state);
}

void Expert::showHelp(Input *option)
{
dimitri's avatar
dimitri committed
439 440 441 442 443 444 445 446 447 448 449 450
  if (!m_inShowHelp)
  {
    m_inShowHelp = TRUE;
    m_helper->setText(
        QString::fromAscii("<qt><b>")+option->id()+
        QString::fromAscii("</b><br>")+
        option->docs().
        replace(QChar::fromAscii('\n'),QChar::fromAscii(' '))+
        QString::fromAscii("<qt>")
        );
    m_inShowHelp = FALSE;
  }
dimitri's avatar
dimitri committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
}

void Expert::nextTopic()
{
  m_topicStack->setCurrentIndex(m_topicStack->currentIndex()+1);
  m_next->setEnabled(m_topicStack->count()!=m_topicStack->currentIndex()+1);
  m_prev->setEnabled(m_topicStack->currentIndex()!=0);
  m_treeWidget->setCurrentItem(m_treeWidget->invisibleRootItem()->child(m_topicStack->currentIndex()));
}

void Expert::prevTopic()
{
  m_topicStack->setCurrentIndex(m_topicStack->currentIndex()-1);
  m_next->setEnabled(m_topicStack->count()!=m_topicStack->currentIndex()+1);
  m_prev->setEnabled(m_topicStack->currentIndex()!=0);
  m_treeWidget->setCurrentItem(m_treeWidget->invisibleRootItem()->child(m_topicStack->currentIndex()));
}

void Expert::resetToDefaults()
{
  //printf("Expert::makeDefaults()\n");
  QHashIterator<QString,Input*> i(m_options);
  while (i.hasNext()) 
dimitri's avatar
dimitri committed
474
  {
dimitri's avatar
dimitri committed
475 476 477 478 479
    i.next();
    if (i.value())
    {
      i.value()->reset();
    }
dimitri's avatar
dimitri committed
480 481 482
  }
}

dimitri's avatar
dimitri committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static bool stringVariantToBool(const QVariant &v)
{
  QString s = v.toString().toLower();
  return s==QString::fromAscii("yes") || s==QString::fromAscii("true") || s==QString::fromAscii("1");
} 

static bool getBoolOption(
    const QHash<QString,Input*>&model,const QString &name)
{
  Input *option = model[name];
  Q_ASSERT(option!=0);
  return stringVariantToBool(option->value());
} 

static QString getStringOption(
    const QHash<QString,Input*>&model,const QString &name)
{
  Input *option = model[name];
  Q_ASSERT(option!=0);
  return option->value().toString();
}


bool Expert::htmlOutputPresent(const QString &workingDir) const
{
  bool generateHtml = getBoolOption(m_options,QString::fromAscii("GENERATE_HTML"));
dimitri's avatar
dimitri committed
509
  if (!generateHtml || workingDir.isEmpty()) return false;
dimitri's avatar
dimitri committed
510 511 512 513 514 515
  QString indexFile = getHtmlOutputIndex(workingDir);
  QFileInfo fi(indexFile);
  return fi.exists() && fi.isFile();
}

QString Expert::getHtmlOutputIndex(const QString &workingDir) const
dimitri's avatar
dimitri committed
516
{
dimitri's avatar
dimitri committed
517 518 519 520 521 522
  QString outputDir = getStringOption(m_options,QString::fromAscii("OUTPUT_DIRECTORY"));
  QString htmlOutputDir = getStringOption(m_options,QString::fromAscii("HTML_OUTPUT"));
  //printf("outputDir=%s\n",qPrintable(outputDir));
  //printf("htmlOutputDir=%s\n",qPrintable(htmlOutputDir));
  QString indexFile = workingDir;
  if (QFileInfo(outputDir).isAbsolute()) // override
dimitri's avatar
dimitri committed
523
  {
dimitri's avatar
dimitri committed
524
    indexFile = outputDir;
dimitri's avatar
dimitri committed
525
  }
dimitri's avatar
dimitri committed
526 527 528 529 530 531 532 533 534
  else // append
  { 
    indexFile += QString::fromAscii("/")+outputDir;
  }
  if (QFileInfo(htmlOutputDir).isAbsolute()) // override
  {
    indexFile = htmlOutputDir;
  }
  else // append
dimitri's avatar
dimitri committed
535
  {
dimitri's avatar
dimitri committed
536
    indexFile += QString::fromAscii("/")+htmlOutputDir;
dimitri's avatar
dimitri committed
537
  }
dimitri's avatar
dimitri committed
538 539
  indexFile+=QString::fromAscii("/index.html");
  return indexFile;
dimitri's avatar
dimitri committed
540 541
}

dimitri's avatar
dimitri committed
542
bool Expert::pdfOutputPresent(const QString &workingDir) const
dimitri's avatar
dimitri committed
543
{
dimitri's avatar
dimitri committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  bool generateLatex = getBoolOption(m_options,QString::fromAscii("GENERATE_LATEX"));
  bool pdfLatex = getBoolOption(m_options,QString::fromAscii("USE_PDFLATEX"));
  if (!generateLatex || !pdfLatex) return false;
  QString latexOutput = getStringOption(m_options,QString::fromAscii("LATEX_OUTPUT"));
  QString indexFile;
  if (QFileInfo(latexOutput).isAbsolute())
  {
    indexFile = latexOutput+QString::fromAscii("/refman.pdf");
  }
  else
  {
    indexFile = workingDir+QString::fromAscii("/")+
                latexOutput+QString::fromAscii("/refman.pdf");
  }
  QFileInfo fi(indexFile);
  return fi.exists() && fi.isFile();
dimitri's avatar
dimitri committed
560 561
}