newcomponentdialog.cpp
1 /*
2  name: tools/editor/newcomponentdialog.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include "newcomponentdialog.h"
30 #include <QLayout>
31 
32 #include <QCompleter>
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QListWidget>
36 #include <QPushButton>
37 #include <QMessageBox>
38 
39 
40 NewComponentDialog::NewComponentDialog(ARCSContext* ctx, QWidget *parent) :
41  QDialog(parent)
42 {
43  context = ctx;
44 
45  this->setLayout(new QVBoxLayout());
46 
47  componentType = new QLineEdit(this);
48  componentName = new QLineEdit(this);
49  layout()->addWidget(new QLabel("Component types:"));
50  layout()->addWidget(componentType);
51 
52  componentTypes = new QListWidget(this);
53  componentTypes->addItems(ARCSFactory::getInstance()->getComponentNames());
54  componentTypes->sortItems();
55  layout()->addWidget(componentTypes);
56 
57  layout()->addWidget(new QLabel("Component name"));
58  layout()->addWidget(componentName);
59 
60  QHBoxLayout* buttonLayout = new QHBoxLayout();
61  ((QBoxLayout*)layout())->addLayout(buttonLayout);
62  okButton = new QPushButton("Ok");
63  cancelButton = new QPushButton("Cancel");
64  buttonLayout->addWidget(okButton);
65  buttonLayout->addWidget(cancelButton);
66 
67  QCompleter* cmpl = new QCompleter(ARCSFactory::getInstance()->getComponentNames(),this);
68  cmpl->setCaseSensitivity(Qt::CaseInsensitive);
69  componentType->setCompleter(cmpl);
70 
71  connect(okButton,SIGNAL(clicked()),this,SLOT(verify()));
72  connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
73  connect(componentType,SIGNAL(textChanged(QString)),this,SLOT(setType(QString)));
74  connect(componentTypes,SIGNAL(currentTextChanged(QString)),componentType,SLOT(setText(QString)));
75 }
76 
77 
78 void NewComponentDialog::verify()
79 {
80  if (componentTypes->selectedItems().isEmpty())
81  {
82  QMessageBox::critical(this,"Missing type","Component type is missing",QMessageBox::Ok);
83  return ;
84  }
85  if (componentName->text().isEmpty() || context->getComponent(componentName->text()) )
86  {
87  QMessageBox::critical(this,"Invalid name","Your component name is either empty or already taken by another one.",QMessageBox::Ok);
88  return;
89  }
90 
91  accept();
92 }
93 
94 void NewComponentDialog::setType(QString s)
95 {
96  QList<QListWidgetItem*> results = componentTypes->findItems(s,Qt::MatchCaseSensitive);
97 
98  if (!results.isEmpty())
99  {
100  componentTypes->scrollToItem(results[0]);
101  componentTypes->setCurrentItem(results[0]);
102  }
103 }
static ARCSFactory * getInstance()
Returns the instance of the singleton ARCSFactory.
ARCSAbstractComponent * getComponent(QString name)
Retrieves a component by its id.
This class manages components and constants in a given context.
Definition: arcscontext.h:45