inputstring.cpp 4.87 KB
Newer Older
1 2
/******************************************************************************
 *
3
 * 
4
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
5
 * Copyright (C) 1997-2013 by Dimitri van Heesch.
6 7 8 9 10 11 12 13 14 15
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 */

#include "inputstring.h"
16 17 18 19 20 21
#include "helplabel.h"
#include "doxywizard.h"
#include "config.h"

#include <QtGui>

22 23 24 25 26 27 28 29 30 31
class NoWheelComboBox : public QComboBox
{
  protected:
    void wheelEvent(QWheelEvent *e)
    {
      e->ignore();
    }
};


32 33
InputString::InputString( QGridLayout *layout,int &row,
                          const QString & id, const QString &s, 
34 35 36 37
                          StringMode m, const QString &docs,
                          const QString &absPath )
  : m_default(s), m_sm(m), m_index(0), m_docs(docs), m_id(id),
    m_absPath(absPath==QString::fromAscii("1"))
38
{
39
  m_lab = new HelpLabel(id);
40 41
  if (m==StringFixed)
  {
42
    layout->addWidget( m_lab, row, 0 );
43
    m_com = new NoWheelComboBox; 
44 45 46 47
    layout->addWidget( m_com, row, 1, 1, 3, Qt::AlignLeft );
    m_le=0;
    m_br=0;
    row++;
48 49 50
  }
  else
  {
51 52 53 54
    layout->addWidget( m_lab, row, 0 );
    m_le = new QLineEdit;
    m_le->setText( s );
    //layout->setColumnMinimumWidth(2,150);
55 56
    if (m==StringFile || m==StringDir)
    {
57 58 59
      layout->addWidget( m_le, row, 1 );
      m_br = new QToolBar;
      m_br->setIconSize(QSize(24,24));
60
      if (m==StringFile) 
61
      {
62 63
        QAction *file = m_br->addAction(QIcon(QString::fromAscii(":/images/file.png")),QString(),this,SLOT(browse()));
        file->setToolTip(tr("Browse to a file"));
64
      }
65
      else 
66
      {
67 68
        QAction *dir = m_br->addAction(QIcon(QString::fromAscii(":/images/folder.png")),QString(),this,SLOT(browse()));
        dir->setToolTip(tr("Browse to a folder"));
69
      }
70
      layout->addWidget( m_br,row,2 );
71 72 73
    }
    else
    {
74 75
      layout->addWidget( m_le, row, 1, 1, 2 );
      m_br=0;
76
    }
77 78
    m_com=0;
    row++;
79 80
  }

81 82 83 84 85 86 87 88 89 90 91 92 93
  if (m_le)  connect( m_le,   SIGNAL(textChanged(const QString&)), 
                      this,   SLOT(setValue(const QString&)) );
  if (m_com) connect( m_com,  SIGNAL(activated(const QString &)), 
                      this,   SLOT(setValue(const QString &)) );
  m_str = s+QChar::fromAscii('!'); // force update
  setValue(s);
  connect( m_lab, SIGNAL(enter()), SLOT(help()) );
  connect( m_lab, SIGNAL(reset()), SLOT(reset()) );
}

void InputString::help()
{
  showHelp(this);
94 95
}

96

97 98 99 100 101
InputString::~InputString()
{
}


102
void InputString::setValue(const QString &s)
103
{
104
  if (m_str!=s)
105
  {
106 107
    m_str = s;
    m_value = m_str;
108 109 110 111 112 113 114
    updateDefault();
  }
}
void InputString::updateDefault()
{
  {
    if (m_str==m_default || !m_lab->isEnabled())
115 116 117 118 119 120 121
    {
      m_lab->setText(QString::fromAscii("<qt>")+m_id+QString::fromAscii("</qt"));
    }
    else
    {
      m_lab->setText(QString::fromAscii("<qt><font color='red'>")+m_id+QString::fromAscii("</font></qt>"));
    }
122
    if (m_le && m_le->text()!=m_str) m_le->setText( m_str );
123 124 125 126 127 128
    emit changed();
  }
}

void InputString::setEnabled(bool state)
{
129 130 131 132
  m_lab->setEnabled(state);
  if (m_le)  m_le->setEnabled(state);
  if (m_br)  m_br->setEnabled(state);
  if (m_com) m_com->setEnabled(state);
133
  updateDefault();
134 135 136 137
}

void InputString::browse()
{
138 139
  QString path = QFileInfo(MainWindow::instance().configFileName()).path();
  if (m_sm==StringFile)
140
  {
141 142
    QString fileName = QFileDialog::getOpenFileName(&MainWindow::instance(),
        tr("Select file"),path);
143 144
    if (!fileName.isNull()) 
    {
145 146
      QDir dir(path);
      if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
147
      {
148
        fileName = m_absPath ? fileName : dir.relativeFilePath(fileName);
149
      }
150
      setValue(fileName);
151 152 153 154
    }
  }
  else // sm==StringDir
  {
155 156
    QString dirName = QFileDialog::getExistingDirectory(&MainWindow::instance(),
        tr("Select directory"),path);
157 158
    if (!dirName.isNull())
    {
159 160
      QDir dir(path);
      if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
161
      {
162
        dirName = m_absPath ? dirName : dir.relativeFilePath(dirName);
163
      }
164
      setValue(dirName);
165
    }
166 167 168 169 170
  }
}

void InputString::clear()
{
171
  setValue(QString());
172 173
}

174
void InputString::addValue(QString s)
175
{
176
  if (m_sm==StringFixed)
177
  {
178 179
    m_values.append(s);
    m_com->addItem(s);
180 181 182
  }
}

183
void InputString::setDefault()
184
{
185 186
  int index = m_values.indexOf(m_str);
  if (index!=-1 && m_com) m_com->setCurrentIndex(index);
187
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

QVariant &InputString::value() 
{
  return m_value;
}

void InputString::update()
{
  setValue(m_value.toString().trimmed());
  setDefault();
}

void InputString::reset()
{
  setValue(m_default);
  setDefault();
}

void InputString::writeValue(QTextStream &t,QTextCodec *codec)
{
  writeStringValue(t,codec,m_str);
}