Skip to content

Commit c6b9df1

Browse files
committed
ticket:4045
Allow users to specify dragged component name.
1 parent fbaa070 commit c6b9df1

File tree

7 files changed

+166
-66
lines changed

7 files changed

+166
-66
lines changed

OMEdit/OMEditGUI/Component/ComponentProperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ void ComponentAttributes::updateComponentAttributes()
12941294
if (!mpComponent->getGraphicsView()->checkComponentName(mpNameTextBox->text())) {
12951295
QMessageBox::information(pModelWidget->getModelWidgetContainer()->getMainWindow(),
12961296
QString(Helper::applicationName).append(" - ").append(Helper::information),
1297-
GUIMessages::getMessage(GUIMessages::SAME_COMPONENT_NAME), Helper::ok);
1297+
GUIMessages::getMessage(GUIMessages::SAME_COMPONENT_NAME).arg(mpNameTextBox->text()), Helper::ok);
12981298
return;
12991299
}
13001300
}

OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,31 @@ bool GraphicsView::addComponent(QString className, QPointF position)
290290
name = defaultName;
291291
} else {
292292
name = getUniqueComponentName(defaultName);
293-
// show the information to the user if we have changed the name of some inner component.
294-
if (defaultPrefix.contains("inner")) {
295-
if (pOptionsDialog->getNotificationsPage()->getInnerModelNameChangedCheckBox()->isChecked()) {
296-
NotificationsDialog *pNotificationsDialog = new NotificationsDialog(NotificationsDialog::InnerModelNameChanged,
297-
NotificationsDialog::InformationIcon,
298-
mpModelWidget->getModelWidgetContainer()->getMainWindow());
299-
pNotificationsDialog->setNotificationLabelString(GUIMessages::getMessage(GUIMessages::INNER_MODEL_NAME_CHANGED)
300-
.arg(defaultName).arg(name));
301-
if (!pNotificationsDialog->exec()) {
302-
return false;
303-
}
293+
}
294+
}
295+
// Allow user to change the component name if always ask for component name settings is true.
296+
if (pOptionsDialog->getNotificationsPage()->getAlwaysAskForDraggedComponentName()->isChecked()) {
297+
ComponentNameDialog *pComponentNameDialog = new ComponentNameDialog(name, this, pMainWindow);
298+
if (pComponentNameDialog->exec()) {
299+
name = pComponentNameDialog->getComponentName();
300+
pComponentNameDialog->deleteLater();
301+
} else {
302+
pComponentNameDialog->deleteLater();
303+
return false;
304+
}
305+
}
306+
// if we or user has changed the default name
307+
if (!defaultName.isEmpty() && name.compare(defaultName) != 0) {
308+
// show the information to the user if we have changed the name of some inner component.
309+
if (defaultPrefix.contains("inner")) {
310+
if (pOptionsDialog->getNotificationsPage()->getInnerModelNameChangedCheckBox()->isChecked()) {
311+
NotificationsDialog *pNotificationsDialog = new NotificationsDialog(NotificationsDialog::InnerModelNameChanged,
312+
NotificationsDialog::InformationIcon,
313+
mpModelWidget->getModelWidgetContainer()->getMainWindow());
314+
pNotificationsDialog->setNotificationLabelString(GUIMessages::getMessage(GUIMessages::INNER_MODEL_NAME_CHANGED)
315+
.arg(defaultName).arg(name));
316+
if (!pNotificationsDialog->exec()) {
317+
return false;
304318
}
305319
}
306320
}
@@ -475,26 +489,39 @@ Component* GraphicsView::getComponentObject(QString componentName)
475489
return 0;
476490
}
477491

492+
/*!
493+
* \brief GraphicsView::getUniqueComponentName
494+
* Creates a unique component name.
495+
* \param componentName
496+
* \param number
497+
* \return
498+
*/
478499
QString GraphicsView::getUniqueComponentName(QString componentName, int number)
479500
{
480501
QString name;
481502
name = QString(componentName).append(QString::number(number));
482-
foreach (Component *pComponent, mComponentsList)
483-
{
484-
if (pComponent->getName().compare(name, Qt::CaseSensitive) == 0)
485-
{
503+
foreach (Component *pComponent, mComponentsList) {
504+
if (pComponent->getName().compare(name, Qt::CaseSensitive) == 0) {
486505
name = getUniqueComponentName(componentName, ++number);
487506
break;
488507
}
489508
}
490509
return name;
491510
}
492511

512+
/*!
513+
* \brief GraphicsView::checkComponentName
514+
* Checks if the component with the same name already exists or not.
515+
* \param componentName
516+
* \return
517+
*/
493518
bool GraphicsView::checkComponentName(QString componentName)
494519
{
495-
foreach (Component *pComponent, mComponentsList)
496-
if (pComponent->getName().compare(componentName, Qt::CaseSensitive) == 0)
520+
foreach (Component *pComponent, mComponentsList) {
521+
if (pComponent->getName().compare(componentName, Qt::CaseSensitive) == 0) {
497522
return false;
523+
}
524+
}
498525
return true;
499526
}
500527

OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.cpp

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,8 +1644,7 @@ void CreateNewItemDialog::createNewFileOrFolder()
16441644
*/
16451645
/*!
16461646
* \brief RenameItemDialog::RenameItemDialog
1647-
* \param path
1648-
* \param isFile
1647+
* \param pLibraryTreeItem
16491648
* \param pMainWindow
16501649
*/
16511650
RenameItemDialog::RenameItemDialog(LibraryTreeItem *pLibraryTreeItem, MainWindow *pMainWindow)
@@ -1756,3 +1755,75 @@ void RenameItemDialog::renameItem()
17561755
}
17571756
accept();
17581757
}
1758+
1759+
/*!
1760+
* \class ComponentNameDialog
1761+
* \brief Creates a dialog to allow users to specify a component name.
1762+
*/
1763+
/*!
1764+
* \brief ComponentNameDialog::ComponentNameDialog
1765+
* \param name
1766+
* \param pGraphicsView
1767+
* \param pMainWindow
1768+
*/
1769+
ComponentNameDialog::ComponentNameDialog(QString name, GraphicsView *pGraphicsView, MainWindow *pMainWindow)
1770+
: QDialog(pMainWindow), mpGraphicsView(pGraphicsView), mpMainWindow(pMainWindow)
1771+
{
1772+
setWindowTitle(tr("%1 - Enter Component Name").arg(Helper::applicationName));
1773+
setMinimumWidth(400);
1774+
Label *pNoteLabel = new Label(tr("Please choose a meaningful name for this component, to improve the readability of simulation results."));
1775+
pNoteLabel->setElideMode(Qt::ElideMiddle);
1776+
// Create the name label and text box
1777+
mpNameLabel = new Label(Helper::name);
1778+
mpNameTextBox = new QLineEdit(name);
1779+
// don't show this message again checkbox.
1780+
mpDontShowThisMessageAgainCheckBox = new QCheckBox(Helper::dontShowThisMessageAgain);
1781+
// Create the buttons
1782+
mpOkButton = new QPushButton(Helper::ok);
1783+
mpOkButton->setAutoDefault(true);
1784+
connect(mpOkButton, SIGNAL(clicked()), SLOT(updateComponentName()));
1785+
mpCancelButton = new QPushButton(Helper::cancel);
1786+
mpCancelButton->setAutoDefault(false);
1787+
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
1788+
// create buttons box
1789+
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
1790+
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
1791+
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
1792+
// Create a layout
1793+
QGridLayout *pMainLayout = new QGridLayout;
1794+
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
1795+
pMainLayout->addWidget(pNoteLabel, 0, 0, 1, 2);
1796+
pMainLayout->addWidget(mpNameLabel, 1, 0);
1797+
pMainLayout->addWidget(mpNameTextBox, 1, 1);
1798+
QHBoxLayout *pHorizontalLayout = new QHBoxLayout;
1799+
pHorizontalLayout->addWidget(mpDontShowThisMessageAgainCheckBox, 0, Qt::AlignLeft);
1800+
pHorizontalLayout->addWidget(mpButtonBox, 0, Qt::AlignRight);
1801+
pMainLayout->addLayout(pHorizontalLayout, 2, 0, 1, 2);
1802+
setLayout(pMainLayout);
1803+
}
1804+
1805+
/*!
1806+
* \brief ComponentNameDialog::updateComponentName
1807+
* Specifies a name for a component.\n
1808+
* Slot activated when mpOkButton clicked signal is raised.
1809+
*/
1810+
void ComponentNameDialog::updateComponentName()
1811+
{
1812+
// check name
1813+
if (mpNameTextBox->text().isEmpty()) {
1814+
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error), GUIMessages::getMessage(
1815+
GUIMessages::ENTER_NAME).arg(Helper::item), Helper::ok);
1816+
return;
1817+
}
1818+
if (!mpGraphicsView->checkComponentName(mpNameTextBox->text())) {
1819+
QMessageBox::information(mpMainWindow, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::information),
1820+
GUIMessages::getMessage(GUIMessages::SAME_COMPONENT_NAME).arg(mpNameTextBox->text()), Helper::ok);
1821+
return;
1822+
}
1823+
if (mpDontShowThisMessageAgainCheckBox->isChecked()) {
1824+
QSettings *pSettings = Utilities::getApplicationSettings();
1825+
pSettings->setValue("notifications/alwaysAskForDraggedComponentName", false);
1826+
mpMainWindow->getOptionsDialog()->getNotificationsPage()->getAlwaysAskForDraggedComponentName()->setChecked(false);
1827+
}
1828+
accept();
1829+
}

OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,23 @@ private slots:
329329
void renameItem();
330330
};
331331

332+
class ComponentNameDialog : public QDialog
333+
{
334+
Q_OBJECT
335+
public:
336+
ComponentNameDialog(QString name, GraphicsView *pGraphicsView, MainWindow *pMainWindow);
337+
QString getComponentName() {return mpNameTextBox->text();}
338+
private:
339+
GraphicsView *mpGraphicsView;
340+
MainWindow *mpMainWindow;
341+
Label *mpNameLabel;
342+
QLineEdit *mpNameTextBox;
343+
QCheckBox *mpDontShowThisMessageAgainCheckBox;
344+
QPushButton *mpOkButton;
345+
QPushButton *mpCancelButton;
346+
QDialogButtonBox *mpButtonBox;
347+
private slots:
348+
void updateComponentName();
349+
};
350+
332351
#endif // MODELICACLASSDIALOG_H

OMEdit/OMEditGUI/Options/OptionsDialog.cpp

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -507,26 +507,24 @@ void OptionsDialog::readMessagesSettings()
507507
//! Reads the Notifications section settings from omedit.ini
508508
void OptionsDialog::readNotificationsSettings()
509509
{
510-
if (mpSettings->contains("notifications/promptQuitApplication"))
511-
{
510+
if (mpSettings->contains("notifications/promptQuitApplication")) {
512511
mpNotificationsPage->getQuitApplicationCheckBox()->setChecked(mpSettings->value("notifications/promptQuitApplication").toBool());
513512
}
514-
if (mpSettings->contains("notifications/itemDroppedOnItself"))
515-
{
513+
if (mpSettings->contains("notifications/itemDroppedOnItself")) {
516514
mpNotificationsPage->getItemDroppedOnItselfCheckBox()->setChecked(mpSettings->value("notifications/itemDroppedOnItself").toBool());
517515
}
518-
if (mpSettings->contains("notifications/replaceableIfPartial"))
519-
{
516+
if (mpSettings->contains("notifications/replaceableIfPartial")) {
520517
mpNotificationsPage->getReplaceableIfPartialCheckBox()->setChecked(mpSettings->value("notifications/replaceableIfPartial").toBool());
521518
}
522-
if (mpSettings->contains("notifications/innerModelNameChanged"))
523-
{
519+
if (mpSettings->contains("notifications/innerModelNameChanged")) {
524520
mpNotificationsPage->getInnerModelNameChangedCheckBox()->setChecked(mpSettings->value("notifications/innerModelNameChanged").toBool());
525521
}
526-
if (mpSettings->contains("notifications/saveModelForBitmapInsertion"))
527-
{
522+
if (mpSettings->contains("notifications/saveModelForBitmapInsertion")) {
528523
mpNotificationsPage->getSaveModelForBitmapInsertionCheckBox()->setChecked(mpSettings->value("notifications/saveModelForBitmapInsertion").toBool());
529524
}
525+
if (mpSettings->contains("notifications/alwaysAskForDraggedComponentName")) {
526+
mpNotificationsPage->getAlwaysAskForDraggedComponentName()->setChecked(mpSettings->value("notifications/alwaysAskForDraggedComponentName").toBool());
527+
}
530528
}
531529

532530
//! Reads the LineStyle section settings from omedit.ini
@@ -961,6 +959,7 @@ void OptionsDialog::saveNotificationsSettings()
961959
mpSettings->setValue("notifications/replaceableIfPartial", mpNotificationsPage->getReplaceableIfPartialCheckBox()->isChecked());
962960
mpSettings->setValue("notifications/innerModelNameChanged", mpNotificationsPage->getInnerModelNameChangedCheckBox()->isChecked());
963961
mpSettings->setValue("notifications/saveModelForBitmapInsertion", mpNotificationsPage->getSaveModelForBitmapInsertionCheckBox()->isChecked());
962+
mpSettings->setValue("notifications/alwaysAskForDraggedComponentName", mpNotificationsPage->getAlwaysAskForDraggedComponentName()->isChecked());
964963
}
965964

966965
//! Saves the LineStyle section settings to omedit.ini
@@ -3259,11 +3258,14 @@ void MessagesPage::pickErrorColor()
32593258
setErrorPickColorButtonIcon();
32603259
}
32613260

3262-
//! @class NotificationsPage
3263-
//! @brief Creates an interface for Notifications settings.
3264-
3265-
//! Constructor
3266-
//! @param pOptionsDialog is the pointer to OptionsDialog
3261+
/*!
3262+
* \class NotificationsPage
3263+
* \brief Creates an interface for Notifications settings.
3264+
*/
3265+
/*!
3266+
* \brief NotificationsPage::NotificationsPage
3267+
* \param pOptionsDialog is the pointer to OptionsDialog
3268+
*/
32673269
NotificationsPage::NotificationsPage(OptionsDialog *pOptionsDialog)
32683270
: QWidget(pOptionsDialog)
32693271
{
@@ -3284,6 +3286,9 @@ NotificationsPage::NotificationsPage(OptionsDialog *pOptionsDialog)
32843286
// create the save model for bitmap insertion checkbox
32853287
mpSaveModelForBitmapInsertionCheckBox = new QCheckBox(tr("Show save model for bitmap insertion message"));
32863288
mpSaveModelForBitmapInsertionCheckBox->setChecked(true);
3289+
// create the save model for bitmap insertion checkbox
3290+
mpAlwaysAskForDraggedComponentName = new QCheckBox(tr("Always ask for the dragged component name"));
3291+
mpAlwaysAskForDraggedComponentName->setChecked(true);
32873292
// set the layout of notifications group
32883293
QGridLayout *pNotificationsLayout = new QGridLayout;
32893294
pNotificationsLayout->setAlignment(Qt::AlignTop);
@@ -3292,6 +3297,7 @@ NotificationsPage::NotificationsPage(OptionsDialog *pOptionsDialog)
32923297
pNotificationsLayout->addWidget(mpReplaceableIfPartialCheckBox, 2, 0);
32933298
pNotificationsLayout->addWidget(mpInnerModelNameChangedCheckBox, 3, 0);
32943299
pNotificationsLayout->addWidget(mpSaveModelForBitmapInsertionCheckBox, 4, 0);
3300+
pNotificationsLayout->addWidget(mpAlwaysAskForDraggedComponentName, 5, 0);
32953301
mpNotificationsGroupBox->setLayout(pNotificationsLayout);
32963302
// set the layout
32973303
QVBoxLayout *pLayout = new QVBoxLayout;
@@ -3301,31 +3307,6 @@ NotificationsPage::NotificationsPage(OptionsDialog *pOptionsDialog)
33013307
setLayout(pLayout);
33023308
}
33033309

3304-
QCheckBox* NotificationsPage::getQuitApplicationCheckBox()
3305-
{
3306-
return mpQuitApplicationCheckBox;
3307-
}
3308-
3309-
QCheckBox* NotificationsPage::getItemDroppedOnItselfCheckBox()
3310-
{
3311-
return mpItemDroppedOnItselfCheckBox;
3312-
}
3313-
3314-
QCheckBox* NotificationsPage::getReplaceableIfPartialCheckBox()
3315-
{
3316-
return mpReplaceableIfPartialCheckBox;
3317-
}
3318-
3319-
QCheckBox* NotificationsPage::getInnerModelNameChangedCheckBox()
3320-
{
3321-
return mpInnerModelNameChangedCheckBox;
3322-
}
3323-
3324-
QCheckBox* NotificationsPage::getSaveModelForBitmapInsertionCheckBox()
3325-
{
3326-
return mpSaveModelForBitmapInsertionCheckBox;
3327-
}
3328-
33293310
//! @class LineStylePage
33303311
//! @brief Creates an interface for line style settings.
33313312

OMEdit/OMEditGUI/Options/OptionsDialog.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,12 @@ class NotificationsPage : public QWidget
596596
Q_OBJECT
597597
public:
598598
NotificationsPage(OptionsDialog *pOptionsDialog);
599-
QCheckBox* getQuitApplicationCheckBox();
600-
QCheckBox* getItemDroppedOnItselfCheckBox();
601-
QCheckBox* getReplaceableIfPartialCheckBox();
602-
QCheckBox* getInnerModelNameChangedCheckBox();
603-
QCheckBox* getSaveModelForBitmapInsertionCheckBox();
599+
QCheckBox* getQuitApplicationCheckBox() {return mpQuitApplicationCheckBox;}
600+
QCheckBox* getItemDroppedOnItselfCheckBox() {return mpItemDroppedOnItselfCheckBox;}
601+
QCheckBox* getReplaceableIfPartialCheckBox() {return mpReplaceableIfPartialCheckBox;}
602+
QCheckBox* getInnerModelNameChangedCheckBox() {return mpInnerModelNameChangedCheckBox;}
603+
QCheckBox* getSaveModelForBitmapInsertionCheckBox() {return mpSaveModelForBitmapInsertionCheckBox;}
604+
QCheckBox* getAlwaysAskForDraggedComponentName() {return mpAlwaysAskForDraggedComponentName;}
604605
private:
605606
OptionsDialog *mpOptionsDialog;
606607
QGroupBox *mpNotificationsGroupBox;
@@ -609,6 +610,7 @@ class NotificationsPage : public QWidget
609610
QCheckBox *mpReplaceableIfPartialCheckBox;
610611
QCheckBox *mpInnerModelNameChangedCheckBox;
611612
QCheckBox *mpSaveModelForBitmapInsertionCheckBox;
613+
QCheckBox *mpAlwaysAskForDraggedComponentName;
612614
};
613615

614616
class LineStylePage : public QWidget

OMEdit/OMEditGUI/Util/Helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ QString GUIMessages::getMessage(int type)
539539
case CHECK_MESSAGES_BROWSER:
540540
return tr("Please check the Messages Browser for more error specific details.");
541541
case SAME_COMPONENT_NAME:
542-
return tr("A Component with the same name already exists. Please choose another Name.");
542+
return tr("A Component with the name <b>%1</b> already exists. Please choose another name.");
543543
case SAME_COMPONENT_CONNECT:
544544
return tr("You cannot connect a component to itself.");
545545
case NO_MODELICA_CLASS_OPEN:

0 commit comments

Comments
 (0)