summaryrefslogtreecommitdiffstats
path: root/addon/doxywizard/inputstring.cpp
blob: 36fe319cde8e23592a39ef49f2a8671a45d36118 (plain)
1
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/******************************************************************************
 *
 * 
 *
 * Copyright (C) 1997-2013 by Dimitri van Heesch.
 *
 * 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"
#include "helplabel.h"
#include "doxywizard.h"
#include "config.h"

#include <QtGui>

class NoWheelComboBox : public QComboBox
{
  protected:
    void wheelEvent(QWheelEvent *e)
    {
      e->ignore();
    }
};


InputString::InputString( QGridLayout *layout,int &row,
                          const QString & id, const QString &s, 
                          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"))
{
  m_lab = new HelpLabel(id);
  if (m==StringFixed)
  {
    layout->addWidget( m_lab, row, 0 );
    m_com = new NoWheelComboBox; 
    layout->addWidget( m_com, row, 1, 1, 3, Qt::AlignLeft );
    m_le=0;
    m_br=0;
    row++;
  }
  else
  {
    layout->addWidget( m_lab, row, 0 );
    m_le = new QLineEdit;
    m_le->setText( s );
    //layout->setColumnMinimumWidth(2,150);
    if (m==StringFile || m==StringDir)
    {
      layout->addWidget( m_le, row, 1 );
      m_br = new QToolBar;
      m_br->setIconSize(QSize(24,24));
      if (m==StringFile) 
      {
        QAction *file = m_br->addAction(QIcon(QString::fromAscii(":/images/file.png")),QString(),this,SLOT(browse()));
        file->setToolTip(tr("Browse to a file"));
      }
      else 
      {
        QAction *dir = m_br->addAction(QIcon(QString::fromAscii(":/images/folder.png")),QString(),this,SLOT(browse()));
        dir->setToolTip(tr("Browse to a folder"));
      }
      layout->addWidget( m_br,row,2 );
    }
    else
    {
      layout->addWidget( m_le, row, 1, 1, 2 );
      m_br=0;
    }
    m_com=0;
    row++;
  }

  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);
}


InputString::~InputString()
{
}


void InputString::setValue(const QString &s)
{
  if (m_str!=s)
  {
    m_str = s;
    m_value = m_str;
    updateDefault();
  }
}
void InputString::updateDefault()
{
  {
    if (m_str==m_default || !m_lab->isEnabled())
    {
      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>"));
    }
    if (m_le && m_le->text()!=m_str) m_le->setText( m_str );
    emit changed();
  }
}

void InputString::setEnabled(bool state)
{
  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);
  updateDefault();
}

void InputString::browse()
{
  QString path = QFileInfo(MainWindow::instance().configFileName()).path();
  if (m_sm==StringFile)
  {
    QString fileName = QFileDialog::getOpenFileName(&MainWindow::instance(),
        tr("Select file"),path);
    if (!fileName.isNull()) 
    {
      QDir dir(path);
      if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
      {
        fileName = m_absPath ? fileName : dir.relativeFilePath(fileName);
      }
      setValue(fileName);
    }
  }
  else // sm==StringDir
  {
    QString dirName = QFileDialog::getExistingDirectory(&MainWindow::instance(),
        tr("Select directory"),path);
    if (!dirName.isNull())
    {
      QDir dir(path);
      if (!MainWindow::instance().configFileName().isEmpty() && dir.exists())
      {
        dirName = m_absPath ? dirName : dir.relativeFilePath(dirName);
      }
      setValue(dirName);
    }
  }
}

void InputString::clear()
{
  setValue(QString());
}

void InputString::addValue(QString s)
{
  if (m_sm==StringFixed)
  {
    m_values.append(s);
    m_com->addItem(s);
  }
}

void InputString::setDefault()
{
  int index = m_values.indexOf(m_str);
  if (index!=-1 && m_com) m_com->setCurrentIndex(index);
}

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);
}