1 // Copyright (C) 2006-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PropertyEditor.hxx"
22 #include <QVBoxLayout>
23 #include <QToolButton>
28 #include <QActionGroup>
29 #include <QHeaderView>
31 #include <QInputDialog>
32 #include <QMessageBox>
33 #include "guiObservers.hxx"
34 #include "Message.hxx"
37 #include "YacsTrace.hxx"
39 using namespace YACS::HMI;
40 //using namespace YACS::ENGINE;
42 /*! \class YACS::HMI::PropertyEditor
43 * \brief A class to edit properties of a subject
47 PropertyEditor::PropertyEditor(Subject* subject,QWidget *parent):QWidget(parent),_subject(subject),_editing(false)
49 std::vector<std::string> v=_subject->knownProperties();
50 for (int i=0; i<v.size(); ++i)
52 _knownProperties << QString::fromStdString(v[i]);
55 _removeAction=new QAction(QIcon("icons:icon_minus.png"),"Remove Property",this);
56 _addAction=new QAction(QIcon("icons:icon_plus.png"),"Add Property",this);
58 _table=new QTableWidget;
59 _table->setColumnCount(2);
61 headers << "Name" << "Value";
62 _table->setHorizontalHeaderLabels(headers);
63 _table->verticalHeader()->hide();
64 _table->setSelectionMode(QAbstractItemView::SingleSelection);
66 connect(_table, SIGNAL(itemChanged(QTableWidgetItem *)),this, SLOT(onItemChanged(QTableWidgetItem *)));
67 connect(_removeAction, SIGNAL(triggered()), this, SLOT(onRemoveProperty()));
69 QHBoxLayout* hboxLayout = new QHBoxLayout();
70 hboxLayout->setMargin(0);
71 QToolButton* tb_options = new QToolButton();
72 tb_options->setCheckable(true);
74 icon.addFile("icons:icon_down.png");
75 icon.addFile("icons:icon_up.png", QSize(), QIcon::Normal, QIcon::On);
76 tb_options->setIcon(icon);
77 hboxLayout->addWidget(tb_options);
78 connect(tb_options, SIGNAL(toggled(bool)), this, SLOT(on_tb_options_toggled(bool)));
80 QLabel* label=new QLabel("Properties ");
85 hboxLayout->addWidget(label);
89 QToolButton* button=new QToolButton();
90 button->setDefaultAction(_addAction);
91 button->setPopupMode(QToolButton::InstantPopup);
92 _bar->addWidget(button);
94 button=new QToolButton();
95 button->setDefaultAction(_removeAction);
96 _bar->addWidget(button);
97 hboxLayout->addWidget(_bar);
99 QVBoxLayout *layout = new QVBoxLayout;
100 layout->setMargin(0);
101 layout->addLayout(hboxLayout);
102 layout->addWidget(_table);
114 PropertyEditor::~PropertyEditor()
118 //! Qt slot to remove the selected item in the table
121 void PropertyEditor::onRemoveProperty()
123 DEBTRACE("PropertyEditor::onRemoveProperty");
124 QList<QTableWidgetItem *> litems=_table->selectedItems();
127 QTableWidgetItem *item =litems.first();
128 _propertyNames.removeOne(item->text());
129 _table->removeRow(_table->row(item));
134 //! Qt slot to add a property item in the table
136 * \param action : the triggered action
138 void PropertyEditor::onAddProperty(QAction* action)
140 DEBTRACE("PropertyEditor::onAddProperty " << action->text().toStdString());
142 QString text=action->text();
143 if(text == "Other ...")
145 text = QInputDialog::getText(this, tr("New Property"),tr("Name:"), QLineEdit::Normal, "", &ok);
146 if (!ok || text.isEmpty())
150 if(_propertyNames.contains(text))
152 QMessageBox::warning ( 0, "Property already defined", "Property already defined");
157 int row=_table->rowCount();
158 _table->setRowCount(row+1);
160 QTableWidgetItem *newItem = new QTableWidgetItem(text);
161 newItem->setFlags(newItem->flags() & ~Qt::ItemIsEditable);
162 _table->setItem(row,0, newItem);
164 newItem = new QTableWidgetItem();
165 _table->setItem(row,1, newItem);
166 _propertyNames << text;
171 //! Qt slot to change the value of a property item in the table
173 * \param item : the item changed
175 void PropertyEditor::onItemChanged(QTableWidgetItem * item)
177 DEBTRACE("PropertyEditor::onItemChanged " << _editing);
183 //! Update the property items with their values in the subject
186 void PropertyEditor::update()
188 DEBTRACE("PropertyEditor::update " );
190 QTableWidgetItem *newItem;
192 _table->setRowCount(0);
193 _propertyNames << QStringList();
194 std::map<std::string,std::string> props=_subject->getProperties();
195 for (std::map<std::string, std::string>::iterator it = props.begin(); it != props.end(); ++it)
197 _table->setRowCount(row+1);
198 QTableWidgetItem *newItem = new QTableWidgetItem(it->first.c_str());
199 newItem->setFlags(newItem->flags() & ~Qt::ItemIsEditable);
200 _table->setItem(row,0, newItem);
202 newItem = new QTableWidgetItem(it->second.c_str());
203 _table->setItem(row,1, newItem);
204 _propertyNames << it->first.c_str();
210 //! Update the menu used to add properties with the already set properties
213 void PropertyEditor::updateMenu()
215 DEBTRACE("PropertyEditor::updateMenu " );
216 QMenu* menu=new QMenu;
217 _addAction->setMenu(menu);
219 QActionGroup* actGroup=new QActionGroup(this);
220 connect(actGroup, SIGNAL(triggered(QAction*)), this, SLOT(onAddProperty(QAction*)));
223 for (int i = 0; i < _knownProperties.size(); ++i)
225 if(_propertyNames.contains(_knownProperties.at(i)))
227 anAction= actGroup->addAction(_knownProperties.at(i));
228 anAction->setData(QVariant(_knownProperties.at(i)));
229 menu->addAction(anAction);
231 anAction= actGroup->addAction("Other ...");
232 menu->addAction(anAction);
233 anAction->setData(QVariant("..."));
236 //! Update the subject properties with the values stored in the table
239 void PropertyEditor::setProperties()
241 DEBTRACE("PropertyEditor::setProperties " );
242 QTableWidgetItem *item;
245 std::map<std::string,std::string> props;
246 for (int i = 0; i < _table->rowCount(); ++i)
248 item=_table->item(i,0);
249 name=item->data(Qt::DisplayRole).toString().toStdString();
250 item=_table->item(i,1);
251 value=item->data(Qt::DisplayRole).toString().toStdString();
254 bool ret=_subject->setProperties(props);
259 void PropertyEditor::on_tb_options_toggled(bool checked)
261 DEBTRACE("PropertyEditor::on_tb_options_toggled " << checked);