summaryrefslogtreecommitdiffstats
path: root/src/tools/gui/tool_config_widget.cpp
blob: 308e61bc001ec7058f8b57f219e90e46402059bd (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
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *   Copyright (C) 2004 Alain Gibaud <alain.gibaud@free.fr>                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "tool_config_widget.h"

#include <qtooltip.h>
#include <qwidgetstack.h>
#include <qvgroupbox.h>
#include <klocale.h>

#include "devices/base/hex_buffer.h"
#include "tools/list/compile_config.h"
#include "libgui/project.h"
#include "common/gui/purl_gui.h"
#include "tools/list/compile_process.h"

const char * const ToolConfigWidget::ARGUMENTS_TYPE_LABELS[Nb_ArgumentsTypes] = {
  I18N_NOOP("Automatic"), I18N_NOOP("Custom")
};

ToolConfigWidget::ToolConfigWidget(Project *project)
  : ::ConfigWidget(0), _group(0), _project(project),
    _customOptions(0), _customLibraries(0), _includeDirs(0), _hexFormat(0),
    _config(0), _tmpConfig(0), _process(0), _tmpProject(0)
{

  Container *container = new Container(this);
  container->setColStretch(2, 1);
  addWidget(container, 0,0, 0,0);
  QLabel *label = new QLabel(i18n("Configuration:"), container);
  container->addWidget(label, 0,0, 0,0);
  _argumentsType = new KComboBox(container);
  for (uint i=0; i<Nb_ArgumentsTypes; i++)
    _argumentsType->insertItem(i18n(ARGUMENTS_TYPE_LABELS[i]), i);
  connect(_argumentsType, SIGNAL(activated(int)), SLOT(updateArguments()));
  container->addWidget(_argumentsType, 0,0, 1,1);
  label = new QLabel(i18n("Arguments:"), container);
  container->addWidget(label, 1,1, 0,0);
  _arguments = new KLineEdit(container);
  _arguments->setReadOnly(true);
  container->addWidget(_arguments, 1,1, 1,2);
  KPushButton *button = new KPushButton(KGuiItem(QString::null, "help"), container);
  connect(button, SIGNAL(clicked()), SIGNAL(displayHelp()));
  container->addWidget(button, 1,1, 3,3);
  _argumentsEditor = new EditListBox(1, container, "arguments_editor", EditListBox::DuplicatesAllowed,
                                     EditListBox::Add | EditListBox::Remove | EditListBox::UpDown | EditListBox::RemoveAll | EditListBox::Reset);
  connect(_argumentsEditor, SIGNAL(changed()), SLOT(updateArguments()));
  connect(_argumentsEditor, SIGNAL(reset()), SLOT(resetCustomArguments()));
  container->addWidget(_argumentsEditor, 2,2, 0,3);

  _container = new Container(container);
  _container->setColStretch(2, 1);
  container->addWidget(_container, 3,3, 0,3);

  connect(this, SIGNAL(changed()), SLOT(updateArguments()));
}

void ToolConfigWidget::init(Tool::Category category, const Tool::Group &group)
{
  _category = category;
  _group = &group;
  _config = _group->createConfig(_project);
  _tmpProject = new Project(PURL::Url());
  _tmpConfig = _group->createConfig(_tmpProject);
  Compile::Data data(_category, QValueList<Compile::TodoItem>(), QString::null, _project, Compile::NormalLinking);
  _process = _group->createCompileProcess(data, 0);

  initEntries();
  createCustomOptionsEntry();
}

ToolConfigWidget::~ToolConfigWidget()
{
  delete _process;
  delete _tmpConfig;
  delete _tmpProject;
  delete _config;
}

PURL::DirectoriesWidget *ToolConfigWidget::createDirectoriesEntry(const QString &text)
{
  uint row = container()->numRows();
  PURL::DirectoriesWidget *sdw = new PURL::DirectoriesWidget(text, _project ? _project->directory().path() : QString::null, container());
  connect(sdw, SIGNAL(changed()), SIGNAL(changed()));
  container()->addWidget(sdw, row,row, 0,2);
  return sdw;
}

void ToolConfigWidget::createCustomOptionsEntry()
{
  uint row = container()->numRows();
  QLabel *label = new QLabel(i18n("Custom options:"), container());
  container()->addWidget(label, row,row, 0,0);
  _customOptions = new QLineEdit(container());
  connect(_customOptions, SIGNAL(textChanged(const QString &)), SIGNAL(changed()));
  container()->addWidget(_customOptions, row,row, 1,2);
}

void ToolConfigWidget::createCustomLibrariesEntry()
{
  uint row = container()->numRows();
  QLabel *label = new QLabel(i18n("Custom libraries:"), container());
  container()->addWidget(label, row,row, 0,0);
  _customLibraries = new QLineEdit(container());
  connect(_customLibraries, SIGNAL(textChanged(const QString &)), SIGNAL(changed()));
  QToolTip::add(_customLibraries, i18n("<qt>This values will be placed after the linked objects.</qt>")) ;
  container()->addWidget(_customLibraries, row,row, 1,2);
}

void ToolConfigWidget::createHexFormatEntry()
{
  uint row = container()->numRows();
  QLabel *label = new QLabel(i18n("Hex file format:"), container());
  container()->addWidget(label, row,row, 0,0);
  _hexFormat = new QComboBox(container());
  connect(_hexFormat, SIGNAL(activated(int)), SIGNAL(changed()));
  for (uint i=0; i<HexBuffer::Nb_Formats; i++)
    _hexFormat->insertItem(HexBuffer::FORMATS[i]);
  _hexFormat->insertItem(i18n("as in LIST directive"));
  container()->addWidget(_hexFormat, row,row, 1,1);
}

void ToolConfigWidget::loadConfig()
{
  loadConfig(*_config);
  if ( _config->customArguments(_category).isEmpty() ) resetCustomArguments();
  else updateArguments();
}

void ToolConfigWidget::resetCustomArguments()
{
  _argumentsEditor->setTexts(arguments(AutomaticArguments));
  updateArguments();
}

void ToolConfigWidget::loadConfig(const Compile::Config &config)
{
  _argumentsType->setCurrentItem(config.hasCustomArguments(_category) ? CustomArguments : AutomaticArguments);
  _argumentsEditor->setTexts(config.customArguments(_category));
  if (_includeDirs) _includeDirs->setDirectories(config.rawIncludeDirs(_category)) ;
  if (_customOptions) _customOptions->setText(config.rawCustomOptions(_category));
  if (_customLibraries) _customLibraries->setText(config.rawCustomLibraries(_category));
  if (_hexFormat) _hexFormat->setCurrentItem(config.hexFormat());
}

void ToolConfigWidget::saveConfig(Compile::Config &config) const
{
  config.setHasCustomArguments(_category, _argumentsType->currentItem()==CustomArguments);
  config.setCustomArguments(_category, _argumentsEditor->texts());
  if (_includeDirs) config.setRawIncludeDirs(_category, _includeDirs->directories());
  if (_customOptions) config.setRawCustomOptions(_category, _customOptions->text());
  if (_customLibraries) config.setRawCustomLibraries(_category, _customLibraries->text());
  if (_hexFormat) config.setHexFormat(HexBuffer::Format(_hexFormat->currentItem()));
}

QStringList ToolConfigWidget::arguments(ArgumentsType type) const
{
  if ( _tmpConfig==0 ) return QStringList();
  saveConfig(*_tmpConfig);
  if ( type==AutomaticArguments ) return _process->genericArguments(*_tmpConfig);
  return _argumentsEditor->texts();
}

void ToolConfigWidget::updateArguments()
{
  ArgumentsType type = ArgumentsType(_argumentsType->currentItem());
  if ( type==AutomaticArguments ) {
    _argumentsEditor->hide();
    _container->show();
  } else {
    _argumentsEditor->show();
    _container->hide();
  }
  _arguments->setText(arguments(type).join(" "));
}