Skip to content

Commit

Permalink
ticket:5128 Added drag and drop for text layer
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #138
  • Loading branch information
adeas31 authored and OpenModelica-Hudson committed Apr 10, 2019
1 parent 18533fb commit 52e77f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
39 changes: 39 additions & 0 deletions OMEdit/OMEdit/OMEditGUI/Editors/BaseEditor.cpp
Expand Up @@ -1811,6 +1811,45 @@ QMimeData* PlainTextEdit::createMimeDataFromSelection() const
return 0;
}

/*!
* \brief PlainTextEdit::canInsertFromMimeData
* Reimplementation of QPlainTextEdit::canInsertFromMimeData.\n
* Checks the QMimeData format and return true if the format is allowed.
* \param source
* \return
*/
bool PlainTextEdit::canInsertFromMimeData(const QMimeData *source) const
{
// check mimeData to see if we can insert from it
if (source->hasFormat(Helper::modelicaComponentFormat)) {
return mpBaseEditor->getModelWidget() && !mpBaseEditor->getModelWidget()->getLibraryTreeItem()->isSystemLibrary();
} else {
return QPlainTextEdit::canInsertFromMimeData(source);
}
}

/*!
* \brief PlainTextEdit::insertFromMimeData
* Checks the QMimeData format and performs a specific operation.
* \param source
*/
void PlainTextEdit::insertFromMimeData(const QMimeData *source)
{
if (source->hasFormat(Helper::modelicaComponentFormat)) {
QByteArray itemData = source->data(Helper::modelicaComponentFormat);
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
QString className;
dataStream >> className;
textCursor().insertFragment(QTextDocumentFragment::fromPlainText(className));
ensureCursorVisible();
setFocus(Qt::ActiveWindowFocusReason);
} else if (source->hasFormat(Helper::modelicaFileFormat)) {
MainWindow::instance()->openDroppedFile(source);
} else {
QPlainTextEdit::insertFromMimeData(source);
}
}

/*!
* \brief PlainTextEdit::focusInEvent
* Reimplementation of QPlainTextEdit::focusInEvent(). Stops the auto save timer.
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEdit/OMEditGUI/Editors/BaseEditor.h
Expand Up @@ -307,6 +307,8 @@ public slots:
virtual void resizeEvent(QResizeEvent *pEvent);
virtual void keyPressEvent(QKeyEvent *pEvent);
virtual QMimeData* createMimeDataFromSelection() const;
virtual bool canInsertFromMimeData(const QMimeData *source) const;
virtual void insertFromMimeData(const QMimeData *source);
virtual void focusInEvent(QFocusEvent *event);
virtual void focusOutEvent(QFocusEvent *event);
void paintEvent(QPaintEvent *e);
Expand Down
10 changes: 5 additions & 5 deletions OMEdit/OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -604,15 +604,15 @@ void MainWindow::beforeClosingMainWindow()
/*!
* \brief MainWindow::openDroppedFile
* Opens the dropped file.
* \param event
* \param pMimeData
*/
void MainWindow::openDroppedFile(QDropEvent *event)
void MainWindow::openDroppedFile(const QMimeData *pMimeData)
{
int progressValue = 0;
mpProgressBar->setRange(0, event->mimeData()->urls().size());
mpProgressBar->setRange(0, pMimeData->urls().size());
showProgressBar();
//retrieves the filenames of all the dragged files in list and opens the valid files.
foreach (QUrl fileUrl, event->mimeData()->urls()) {
foreach (QUrl fileUrl, pMimeData->urls()) {
QFileInfo fileInfo(fileUrl.toLocalFile());
// show file loading message
mpStatusBar->showMessage(QString(Helper::loading).append(": ").append(fileInfo.absoluteFilePath()));
Expand Down Expand Up @@ -4373,7 +4373,7 @@ void MainWindow::dropEvent(QDropEvent *event)
event->ignore();
return;
}
openDroppedFile(event);
openDroppedFile(event->mimeData());
event->accept();
}

Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEdit/OMEditGUI/MainWindow.h
Expand Up @@ -51,6 +51,7 @@ extern "C" {
#include <QMainWindow>
#include <QDialog>
#include <QProgressBar>
#include <QMimeData>
#include <QDomDocument>
#include <QStackedWidget>
#include <QActionGroup>
Expand Down Expand Up @@ -207,7 +208,7 @@ class MainWindow : public QMainWindow
void closeEvent(QCloseEvent *event);
int askForExit();
void beforeClosingMainWindow();
void openDroppedFile(QDropEvent *event);
void openDroppedFile(const QMimeData *pMimeData);
void openResultFiles(QStringList fileNames);
void simulate(LibraryTreeItem *pLibraryTreeItem);
void simulateWithTransformationalDebugger(LibraryTreeItem *pLibraryTreeItem);
Expand Down
8 changes: 6 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -2138,7 +2138,7 @@ void GraphicsView::dropEvent(QDropEvent *event)
event->ignore();
return;
} else if (event->mimeData()->hasFormat(Helper::modelicaFileFormat)) {
pMainWindow->openDroppedFile(event);
pMainWindow->openDroppedFile(event->mimeData());
event->accept();
} else if (event->mimeData()->hasFormat(Helper::modelicaComponentFormat)) {
// check if the class is system library
Expand Down Expand Up @@ -6688,8 +6688,12 @@ bool ModelWidgetContainer::eventFilter(QObject *object, QEvent *event)
/* If focus is set to LibraryTreeView, DocumentationViewer, QMenuBar etc. then try to validate the text because user might have
* updated the text manually.
*/
/* Don't check LibraryTreeView focus since now OMEdit supports drag and drop of classnames on text view See ticket:5128
* The user is expected to click on LibraryTreeView and drag items on the working text view which might be invalid.
* So we don't want to validate text in that case.
*/
if ((event->type() == QEvent::MouseButtonPress && qobject_cast<QMenuBar*>(object)) ||
(event->type() == QEvent::FocusIn && (qobject_cast<LibraryTreeView*>(object) || qobject_cast<DocumentationViewer*>(object)))) {
(event->type() == QEvent::FocusIn && qobject_cast<DocumentationViewer*>(object))) {
ModelWidget *pModelWidget = getCurrentModelWidget();
if (pModelWidget && pModelWidget->getLibraryTreeItem()) {
LibraryTreeItem *pLibraryTreeItem = pModelWidget->getLibraryTreeItem();
Expand Down

0 comments on commit 52e77f7

Please sign in to comment.