Skip to content

Commit

Permalink
Renaming file/folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Jun 16, 2016
1 parent a08c6ff commit db87fd0
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 42 deletions.
23 changes: 18 additions & 5 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -3016,23 +3016,36 @@ void LibraryTreeView::createNewFile()
if (!pLibraryTreeItem) {
return;
}
CreateNewItem *pCreateNewItem = new CreateNewItem(pLibraryTreeItem->getFileName(), true, mpLibraryWidget->getMainWindow());
pCreateNewItem->exec();
CreateNewItemDialog *pCreateNewItemDialog = new CreateNewItemDialog(pLibraryTreeItem->getFileName(), true, mpLibraryWidget->getMainWindow());
pCreateNewItemDialog->exec();
}

/*!
* \brief LibraryTreeView::createNewFolder
* Creates a new folder.
*/
void LibraryTreeView::createNewFolder()
{
LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (!pLibraryTreeItem) {
return;
}
CreateNewItem *pCreateNewItem = new CreateNewItem(pLibraryTreeItem->getFileName(), false, mpLibraryWidget->getMainWindow());
pCreateNewItem->exec();
CreateNewItemDialog *pCreateNewItemDialog = new CreateNewItemDialog(pLibraryTreeItem->getFileName(), false, mpLibraryWidget->getMainWindow());
pCreateNewItemDialog->exec();
}

/*!
* \brief LibraryTreeView::renameFileOrFolder
* Renames the file/folder.
*/
void LibraryTreeView::renameFileOrFolder()
{

LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (!pLibraryTreeItem) {
return;
}
RenameItemDialog *pRenameItemDialog = new RenameItemDialog(pLibraryTreeItem->getFileName(), false, mpLibraryWidget->getMainWindow());
pRenameItemDialog->exec();
}

/*!
Expand Down
55 changes: 27 additions & 28 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -4031,55 +4031,54 @@ void ModelWidgetContainer::addModelWidget(ModelWidget *pModelWidget, bool checkP
}
}

/*!
* \brief ModelWidgetContainer::getCurrentModelWidget
* Returns the current ModelWidget.
* \return
*/
ModelWidget* ModelWidgetContainer::getCurrentModelWidget()
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0)
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
else
} else {
return qobject_cast<ModelWidget*>(subWindowList(QMdiArea::ActivationHistoryOrder).last()->widget());
}
}

/*!
* \brief ModelWidgetContainer::getCurrentMdiSubWindow
* Returns the current QMdiSubWindow.
* \return
*/
QMdiSubWindow* ModelWidgetContainer::getCurrentMdiSubWindow()
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0)
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
else
} else {
return subWindowList(QMdiArea::ActivationHistoryOrder).last();
}
}

/*!
* \brief ModelWidgetContainer::getMdiSubWindow
* Returns the QMdiSubWindow for a specific ModelWidget.
* \param pModelWidget
* \return
*/
QMdiSubWindow* ModelWidgetContainer::getMdiSubWindow(ModelWidget *pModelWidget)
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0)
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
}
QList<QMdiSubWindow*> mdiSubWindowsList = subWindowList(QMdiArea::ActivationHistoryOrder);
foreach (QMdiSubWindow *pMdiSubWindow, mdiSubWindowsList)
{
if (pMdiSubWindow->widget() == pModelWidget)
foreach (QMdiSubWindow *pMdiSubWindow, mdiSubWindowsList) {
if (pMdiSubWindow->widget() == pModelWidget) {
return pMdiSubWindow;
}
}
return 0;
}

void ModelWidgetContainer::setPreviousViewType(StringHandler::ViewType viewType)
{
mPreviousViewType = viewType;
}

StringHandler::ViewType ModelWidgetContainer::getPreviousViewType()
{
return mPreviousViewType;
}

void ModelWidgetContainer::setShowGridLines(bool On)
{
mShowGridLines = On;
}

bool ModelWidgetContainer::isShowGridLines()
{
return mShowGridLines;
}

bool ModelWidgetContainer::eventFilter(QObject *object, QEvent *event)
{
if (!object || isHidden() || qApp->activeWindow() != mpMainWindow) {
Expand Down
8 changes: 4 additions & 4 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -436,10 +436,10 @@ class ModelWidgetContainer : public MdiArea
ModelWidget* getCurrentModelWidget();
QMdiSubWindow* getCurrentMdiSubWindow();
QMdiSubWindow* getMdiSubWindow(ModelWidget *pModelWidget);
void setPreviousViewType(StringHandler::ViewType viewType);
StringHandler::ViewType getPreviousViewType();
void setShowGridLines(bool On);
bool isShowGridLines();
void setPreviousViewType(StringHandler::ViewType viewType) {mPreviousViewType = viewType;}
StringHandler::ViewType getPreviousViewType() {return mPreviousViewType;}
void setShowGridLines(bool On) {mShowGridLines = On;}
bool isShowGridLines() {return mShowGridLines;}
bool eventFilter(QObject *object, QEvent *event);
void changeRecentModelsListSelection(bool moveDown);
private:
Expand Down
128 changes: 125 additions & 3 deletions OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -1511,7 +1511,17 @@ void ExportFigaroDialog::exportModelFigaro()
accept();
}

CreateNewItem::CreateNewItem(QString path, bool isCreateFile, MainWindow *pMainWindow)
/*!
* \class CreateNewItemDialog
* \brief Creates a dialog to allow users to create new file/folder.
*/
/*!
* \brief CreateNewItemDialog::CreateNewItemDialog
* \param path
* \param isCreateFile
* \param pMainWindow
*/
CreateNewItemDialog::CreateNewItemDialog(QString path, bool isCreateFile, MainWindow *pMainWindow)
: QDialog(pMainWindow), mPath(path), mIsCreateFile(isCreateFile), mpMainWindow(pMainWindow)
{
setAttribute(Qt::WA_DeleteOnClose);
Expand Down Expand Up @@ -1549,7 +1559,12 @@ CreateNewItem::CreateNewItem(QString path, bool isCreateFile, MainWindow *pMainW
setLayout(pMainLayout);
}

void CreateNewItem::browsePath()
/*!
* \brief CreateNewItemDialog::browsePath
* Creates a new file/folder.\n
* Slot activated when mpOkButton clicked signal is raised.
*/
void CreateNewItemDialog::browsePath()
{
QString currentPath = mpPathTextBox->text();
QString path = StringHandler::getExistingDirectory(this, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::chooseDirectory),
Expand All @@ -1560,7 +1575,7 @@ void CreateNewItem::browsePath()
mpPathTextBox->setText(path);
}

void CreateNewItem::createNewFileOrFolder()
void CreateNewItemDialog::createNewFileOrFolder()
{
// check name
if (mpNameTextBox->text().isEmpty()) {
Expand All @@ -1580,6 +1595,7 @@ void CreateNewItem::createNewFileOrFolder()
tr("Path <b>%1</b> does not exist.").arg(mpPathTextBox->text()), Helper::ok);
return;
}
// check if file/folder already exists
QString fileOrFolderPath = QString("%1/%2").arg(mpPathTextBox->text()).arg(mpNameTextBox->text());
QFileInfo fileInfo(fileOrFolderPath);
if (fileInfo.exists()) {
Expand Down Expand Up @@ -1624,3 +1640,109 @@ void CreateNewItem::createNewFileOrFolder()
}
accept();
}

/*!
* \class RenameItemDialog
* \brief Creates a dialog to allow users to rename a file/folder.
*/
/*!
* \brief RenameItemDialog::RenameItemDialog
* \param path
* \param isCreateFile
* \param pMainWindow
*/
RenameItemDialog::RenameItemDialog(QString path, bool isCreateFile, MainWindow *pMainWindow)
: QDialog(pMainWindow), mPath(path), mIsCreateFile(isCreateFile), mpMainWindow(pMainWindow)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1 - %2 %3").arg(Helper::applicationName).arg(Helper::rename).arg(mIsCreateFile ? Helper::file : Helper::folder));
setMinimumWidth(400);
// Create the name label and text box
mpNameLabel = new Label(Helper::name);
QFileInfo fileInfo(mPath);
mpNameTextBox = new QLineEdit(fileInfo.fileName());
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(renameFileOrFolder()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpNameLabel, 0, 0);
pMainLayout->addWidget(mpNameTextBox, 0, 1);
pMainLayout->addWidget(mpButtonBox, 1, 0, 1, 2, Qt::AlignRight);
setLayout(pMainLayout);
}

/*!
* \brief RenameItemDialog::updateChildrenPath
* Updates the file path of childrens rescursivly.
* \param pLibraryTreeItem
*/
void RenameItemDialog::updateChildrenPath(LibraryTreeItem *pLibraryTreeItem)
{
for (int i = 0 ; i < pLibraryTreeItem->childrenSize() ; i++) {
LibraryTreeItem *pChildLibraryTreeItem = pLibraryTreeItem->child(i);
QString newPath = QString("%1/%2").arg(pLibraryTreeItem->getFileName()).arg(pChildLibraryTreeItem->getName());
pChildLibraryTreeItem->setNameStructure(newPath);
pChildLibraryTreeItem->setFileName(newPath);
if (pChildLibraryTreeItem->getModelWidget()) {
pChildLibraryTreeItem->getModelWidget()->setModelFilePathLabel(newPath);
}
QFileInfo fileInfo(pChildLibraryTreeItem->getFileName());
if (fileInfo.isDir()) {
updateChildrenPath(pChildLibraryTreeItem);
}
}
}

/*!
* \brief RenameItemDialog::renameFileOrFolder
* Renames a file/folder.\n
* Slot activated when mpOkButton clicked signal is raised.
*/
void RenameItemDialog::renameFileOrFolder()
{
// check name
if (mpNameTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error), GUIMessages::getMessage(
GUIMessages::ENTER_NAME).arg(mIsCreateFile ? Helper::file : Helper::folder), Helper::ok);
return;
}
// check if file/folder already exists
QFileInfo oldFileInfo(mPath);
QString fileOrFolderPath = QString("%1/%2").arg(oldFileInfo.absoluteDir().absolutePath()).arg(mpNameTextBox->text());
QFileInfo fileInfo(fileOrFolderPath);
if (fileInfo.exists()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::error),
GUIMessages::getMessage(GUIMessages::MODEL_ALREADY_EXISTS).arg(mIsCreateFile ? Helper::file : Helper::folder)
.arg(mpNameTextBox->text()).arg(fileInfo.absoluteDir().absolutePath()), Helper::ok);
return;
}
// find the LibraryTreeItem based on path
LibraryTreeModel *pLibraryTreeModel = mpMainWindow->getLibraryWidget()->getLibraryTreeModel();
LibraryTreeItem *pLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mPath);
if (pLibraryTreeItem) {
if (QFile::rename(oldFileInfo.absoluteFilePath(), fileInfo.absoluteFilePath())) {
pLibraryTreeItem->setName(mpNameTextBox->text());
pLibraryTreeItem->setNameStructure(fileInfo.absoluteFilePath());
pLibraryTreeItem->setFileName(fileInfo.absoluteFilePath());
if (pLibraryTreeItem->getModelWidget()) {
pLibraryTreeItem->getModelWidget()->setModelFilePathLabel(fileInfo.absoluteFilePath());
pLibraryTreeItem->getModelWidget()->setWindowTitle(mpNameTextBox->text());
}
// if we have renamed a directory then we need to update the file paths of the nested files.
if (fileInfo.isDir()) {
updateChildrenPath(pLibraryTreeItem);
}
}
}
accept();
}
24 changes: 22 additions & 2 deletions OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.h
Expand Up @@ -288,11 +288,11 @@ public slots:
void exportModelFigaro();
};

class CreateNewItem : public QDialog
class CreateNewItemDialog : public QDialog
{
Q_OBJECT
public:
CreateNewItem(QString path, bool isCreateFile, MainWindow *pMainWindow);
CreateNewItemDialog(QString path, bool isCreateFile, MainWindow *pMainWindow);
private:
QString mPath;
bool mIsCreateFile;
Expand All @@ -310,4 +310,24 @@ private slots:
void createNewFileOrFolder();
};

class RenameItemDialog : public QDialog
{
Q_OBJECT
public:
RenameItemDialog(QString path, bool isCreateFile, MainWindow *pMainWindow);
private:
QString mPath;
bool mIsCreateFile;
MainWindow *mpMainWindow;
Label *mpNameLabel;
QLineEdit *mpNameTextBox;
QPushButton *mpOkButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;

void updateChildrenPath(LibraryTreeItem *pLibraryTreeItem);
private slots:
void renameFileOrFolder();
};

#endif // MODELICACLASSDIALOG_H

0 comments on commit db87fd0

Please sign in to comment.