Skip to content

Commit

Permalink
Added the search functionality in Modelica Text View
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7359 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Dec 12, 2010
1 parent 336f72d commit 4b8b3a4
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 29 deletions.
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Helper.cpp
Expand Up @@ -127,6 +127,8 @@ QString GUIMessages::getMessage(int type)
return "Following Error has occurred while saving component attributes. \n\n %1.";
case CHILD_MODEL_SAVE:
return "The %1 '%2' is contained inside a package. It is automatically saved when you save the package.";
case SEARCH_STRING_NOT_FOUND:
return "The search string '%1' is not found.";
default:
return "";
}
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditGUI/Helper.h
Expand Up @@ -102,7 +102,8 @@ class GUIMessages
SAVED_MODEL,
COMMENT_SAVE_ERROR,
ATTRIBUTES_SAVE_ERROR,
CHILD_MODEL_SAVE
CHILD_MODEL_SAVE,
SEARCH_STRING_NOT_FOUND
};

static QString getMessage(int type);
Expand Down
155 changes: 134 additions & 21 deletions OMEdit/OMEditGUI/ModelicaEditor.cpp
Expand Up @@ -40,9 +40,46 @@ ModelicaEditor::ModelicaEditor(ProjectTab *pParent)
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setTabStopWidth(Helper::tabWidth);
setObjectName(tr("ModelicaEditor"));
// depending on the project tab readonly state set the text view readonly state
setReadOnly(mpParentProjectTab->isReadOnly());
connect(this, SIGNAL(focusOut()), mpParentProjectTab, SLOT(ModelicaEditorTextChanged()));

mpFindWidget = new QWidget;
mpFindWidget->setContentsMargins(0, 0, 0, 0);
mpFindWidget->hide();

mpSearchLabelImage = new QLabel;
mpSearchLabelImage->setPixmap(QPixmap(":/Resources/icons/search.png"));
mpSearchLabel = new QLabel(tr("Search"));
mpSearchTextBox = new QLineEdit;
connect(mpSearchTextBox, SIGNAL(textChanged(QString)), SLOT(updateButtons()));
connect(mpSearchTextBox, SIGNAL(returnPressed()), SLOT(findNextText()));

mpPreviuosButton = new QToolButton;
mpPreviuosButton->setAutoRaise(true);
mpPreviuosButton->setText(tr("Previous"));
mpPreviuosButton->setIcon(QIcon(":/Resources/icons/previous.png"));
mpPreviuosButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
connect(mpPreviuosButton, SIGNAL(clicked()), SLOT(findPreviuosText()));

mpNextButton = new QToolButton;
mpNextButton->setAutoRaise(true);
mpNextButton->setText(tr("Next"));
mpNextButton->setIcon(QIcon(":/Resources/icons/next.png"));
mpNextButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
connect(mpNextButton, SIGNAL(clicked()), SLOT(findNextText()));

mpMatchCaseCheckBox = new QCheckBox(tr("Match case"));
mpMatchWholeWordCheckBox = new QCheckBox(tr("Match whole word"));

mpCloseButton = new QToolButton;
mpCloseButton->setAutoRaise(true);
mpCloseButton->setIcon(QIcon(":/Resources/icons/exit.png"));
connect(mpCloseButton, SIGNAL(clicked()), SLOT(hideFindWidget()));

// make previous and next buttons disabled for first time
updateButtons();
}

QString ModelicaEditor::getModelName()
Expand All @@ -68,6 +105,74 @@ QString ModelicaEditor::getModelName()
return QString();
}

void ModelicaEditor::findText(const QString &text, bool forward)
{
QTextCursor currentTextCursor = textCursor();
QTextDocument::FindFlags options;

if (currentTextCursor.hasSelection())
{
currentTextCursor.setPosition(forward ? currentTextCursor.position() : currentTextCursor.anchor(),
QTextCursor::MoveAnchor);
}

if (!forward)
options |= QTextDocument::FindBackward;

if (mpMatchCaseCheckBox->isChecked())
options |= QTextDocument::FindCaseSensitively;

if (mpMatchWholeWordCheckBox->isChecked())
options |= QTextDocument::FindWholeWords;

bool found = true;
QTextCursor newTextCursor = document()->find(text, currentTextCursor, options);
if (newTextCursor.isNull())
{
QTextCursor ac(document());
ac.movePosition(options & QTextDocument::FindBackward ? QTextCursor::End : QTextCursor::Start);
newTextCursor = document()->find(text, ac, options);
if (newTextCursor.isNull())
{
found = false;
newTextCursor = currentTextCursor;
}
}
setTextCursor(newTextCursor);

if (mpSearchTextBox->text().isEmpty())
found = true;

if (!found)
{
QMessageBox::information(mpParentProjectTab->mpParentProjectTabWidget->mpParentMainWindow,
Helper::applicationName + " - Information",
GUIMessages::getMessage(GUIMessages::SEARCH_STRING_NOT_FOUND).arg(text), "OK");
}
}

void ModelicaEditor::hideFindWidget()
{
mpFindWidget->hide();
}

void ModelicaEditor::updateButtons()
{
const bool enable = !mpSearchTextBox->text().isEmpty();
mpPreviuosButton->setEnabled(enable);
mpNextButton->setEnabled(enable);
}

void ModelicaEditor::findNextText()
{
findText(mpSearchTextBox->text(), true);
}

void ModelicaEditor::findPreviuosText()
{
findText(mpSearchTextBox->text(), false);
}

void ModelicaEditor::focusOutEvent(QFocusEvent *e)
{
QTextEdit::focusOutEvent(e);
Expand Down Expand Up @@ -195,10 +300,36 @@ void ModelicaTextHighlighter::initializeSettings()
rule.mFormat = mQuotationFormat;
mHighlightingRules.append(rule);

mQuotesExpression = QRegExp("\"");
mCommentStartExpression = QRegExp("/\\*");
mCommentEndExpression = QRegExp("\\*/");
}

void ModelicaTextHighlighter::highlightMultiLine(const QString &text, QRegExp &startExpression, QRegExp &endExpression,
QTextCharFormat &format)
{
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = startExpression.indexIn(text);

while (startIndex >= 0)
{
int endIndex = endExpression.indexIn(text, startIndex);
int textLength;
if (endIndex == -1)
{
setCurrentBlockState(1);
textLength = text.length() - startIndex;
}
else
{
textLength = endIndex - startIndex + endExpression.matchedLength();
}
setFormat(startIndex, textLength, format);
startIndex = startExpression.indexIn(text, startIndex + textLength);
}
}

void ModelicaTextHighlighter::highlightBlock(const QString &text)
{
setFormat(0, text.length(), mpModelicaTextSettings->getTextRuleColor());
Expand All @@ -214,27 +345,9 @@ void ModelicaTextHighlighter::highlightBlock(const QString &text)
}
}
setCurrentBlockState(0);

int startIndex = 0;
if (previousBlockState() != 1)
startIndex = mCommentStartExpression.indexIn(text);

while (startIndex >= 0)
{
int endIndex = mCommentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1)
{
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
}
else
{
commentLength = endIndex - startIndex + mCommentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, mMultiLineCommentFormat);
startIndex = mCommentStartExpression.indexIn(text, startIndex + commentLength);
}
highlightMultiLine(text, mCommentStartExpression, mCommentEndExpression, mMultiLineCommentFormat);
// setCurrentBlockState(0);
// highlightMultiLine(text, mQuotesExpression, mQuotesExpression, mQuotationFormat);
}

void ModelicaTextHighlighter::settingsChanged()
Expand Down
18 changes: 18 additions & 0 deletions OMEdit/OMEditGUI/ModelicaEditor.h
Expand Up @@ -44,11 +44,26 @@ class ModelicaEditor : public QTextEdit
public:
ModelicaEditor(ProjectTab *pParent = 0);
QString getModelName();
void findText(const QString &text, bool forward);

ProjectTab *mpParentProjectTab;
QString mLastValidText;
QWidget *mpFindWidget;
QLabel *mpSearchLabelImage;
QLabel *mpSearchLabel;
QLineEdit *mpSearchTextBox;
QToolButton *mpPreviuosButton;
QToolButton *mpNextButton;
QCheckBox *mpMatchCaseCheckBox;
QCheckBox *mpMatchWholeWordCheckBox;
QToolButton *mpCloseButton;
signals:
bool focusOut();
public slots:
void hideFindWidget();
void updateButtons();
void findNextText();
void findPreviuosText();
protected:
virtual void focusOutEvent(QFocusEvent *e);
};
Expand All @@ -61,6 +76,8 @@ class ModelicaTextHighlighter : public QSyntaxHighlighter
public:
ModelicaTextHighlighter(ModelicaTextSettings *pSettings, QTextDocument *pParent = 0);
void initializeSettings();
void highlightMultiLine(const QString &text, QRegExp &startExpression, QRegExp &endExpression,
QTextCharFormat &format);

ModelicaTextSettings *mpModelicaTextSettings;
protected:
Expand All @@ -73,6 +90,7 @@ class ModelicaTextHighlighter : public QSyntaxHighlighter
};
QVector<HighlightingRule> mHighlightingRules;

QRegExp mQuotesExpression;
QRegExp mCommentStartExpression;
QRegExp mCommentEndExpression;

Expand Down
50 changes: 44 additions & 6 deletions OMEdit/OMEditGUI/ProjectTabWidget.cpp
Expand Up @@ -1025,16 +1025,35 @@ ProjectTab::ProjectTab(int modelicaType, int iconType, bool readOnly, bool isChi
mpProjectStatusBar->addPermanentWidget(mpViewTypeLabel, 10);
mpProjectStatusBar->addPermanentWidget(mpModelFilePathLabel, 65);

// create the layout for Modelica Editor
QHBoxLayout *modelicaEditorHorizontalLayout = new QHBoxLayout;
modelicaEditorHorizontalLayout->setContentsMargins(0, 0, 0, 0);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpSearchLabelImage);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpSearchLabel);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpSearchTextBox);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpPreviuosButton);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpNextButton);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpMatchCaseCheckBox);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpMatchWholeWordCheckBox);
modelicaEditorHorizontalLayout->addWidget(mpModelicaEditor->mpCloseButton);
mpModelicaEditor->mpFindWidget->setLayout(modelicaEditorHorizontalLayout);
QVBoxLayout *modelicaEditorVerticalLayout = new QVBoxLayout;
modelicaEditorVerticalLayout->setContentsMargins(0, 0, 0, 0);
modelicaEditorVerticalLayout->addWidget(mpModelicaEditor);
modelicaEditorVerticalLayout->addWidget(mpModelicaEditor->mpFindWidget);
mpModelicaEditorWidget = new QWidget;
mpModelicaEditorWidget->setLayout(modelicaEditorVerticalLayout);

QVBoxLayout *tabLayout = new QVBoxLayout;
tabLayout->setContentsMargins(2, 2, 2, 2);
tabLayout->addWidget(mpProjectStatusBar);
tabLayout->addWidget(mpGraphicsView);
tabLayout->addWidget(mpDiagramGraphicsView);
tabLayout->addWidget(mpModelicaEditor);
tabLayout->addWidget(mpModelicaEditorWidget);
setLayout(tabLayout);

// Hide the modelica text view, icon view and show diagram view
mpModelicaEditor->hide();
mpModelicaEditorWidget->hide();
mpDiagramGraphicsView->hide();
mpIconToolButton->setChecked(true);

Expand Down Expand Up @@ -1099,7 +1118,7 @@ void ProjectTab::showDiagramView(bool checked)
return;
// Enable the main window
emit disableMainWindow(false);
mpModelicaEditor->hide();
mpModelicaEditorWidget->hide();
mpGraphicsView->hide();
mpDiagramGraphicsView->show();
mpViewTypeLabel->setText(StringHandler::getViewType(StringHandler::DIAGRAM));
Expand All @@ -1111,15 +1130,15 @@ void ProjectTab::showIconView(bool checked)
return;
// Enable the main window
emit disableMainWindow(false);
mpModelicaEditor->hide();
mpModelicaEditorWidget->hide();
mpDiagramGraphicsView->hide();
mpGraphicsView->show();
mpViewTypeLabel->setText(StringHandler::getViewType(StringHandler::ICON));
}

void ProjectTab::showModelicaTextView(bool checked)
{
if (!checked or (checked and mpModelicaEditor->isVisible()))
if (!checked or (checked and mpModelicaEditorWidget->isVisible()))
return;
// Disable the main window
emit disableMainWindow(true);
Expand All @@ -1131,7 +1150,8 @@ void ProjectTab::showModelicaTextView(bool checked)
// get the modelica text of the model
mpModelicaEditor->setText(mpParentProjectTabWidget->mpParentMainWindow->mpOMCProxy->list(mModelNameStructure));
mpModelicaEditor->mLastValidText = mpModelicaEditor->toPlainText();
mpModelicaEditor->show();
mpModelicaEditor->setFocus();
mpModelicaEditorWidget->show();
}

bool ProjectTab::loadModelFromText(QString modelName)
Expand Down Expand Up @@ -1963,3 +1983,21 @@ void ProjectTabWidget::disableProjectToolbar()
mToolBarEnabled = false;
}
}

void ProjectTabWidget::keyPressEvent(QKeyEvent *event)
{
ProjectTab *pCurrentTab = getCurrentTab();
if (!pCurrentTab)
return;

if (!pCurrentTab->mpModelicaEditorWidget->isVisible())
return;

if (event->modifiers().testFlag(Qt::ControlModifier) and event->key() == Qt::Key_F)
{
pCurrentTab->mpModelicaEditor->mpFindWidget->show();
pCurrentTab->mpModelicaEditor->mpSearchTextBox->setFocus();
}

QTabWidget::keyPressEvent(event);
}
5 changes: 4 additions & 1 deletion OMEdit/OMEditGUI/ProjectTabWidget.h
Expand Up @@ -162,7 +162,6 @@ class ProjectTab : public QWidget
{
Q_OBJECT
private:
ModelicaEditor *mpModelicaEditor;
ModelicaTextHighlighter *mpModelicaTextHighlighter;
QStatusBar *mpProjectStatusBar;
QButtonGroup *mpViewsButtonGroup;
Expand Down Expand Up @@ -197,6 +196,8 @@ class ProjectTab : public QWidget
GraphicsScene *mpGraphicsScene;
GraphicsView *mpDiagramGraphicsView;
GraphicsScene *mpDiagramGraphicsScene;
QWidget *mpModelicaEditorWidget;
ModelicaEditor *mpModelicaEditor;
QString mModelFileName;
QString mModelName;
QString mModelNameStructure;
Expand Down Expand Up @@ -257,6 +258,8 @@ public slots:
void updateTabIndexes();
void enableProjectToolbar();
void disableProjectToolbar();
protected:
virtual void keyPressEvent(QKeyEvent *event);
};

#endif // PROJECTTABWIDGET_H
8 changes: 8 additions & 0 deletions OMEdit/OMEditGUI/Resources/css/stylesheet.qss
Expand Up @@ -26,6 +26,10 @@ QTreeWidget::item {
QTextEdit, QLineEdit {
border: 1px solid gray;
}
QTextEdit#ModelicaEditor {
selection-background-color: #3399FF;
selection-color: white;
}

QGraphicsView {
border: 1px solid gray;
Expand All @@ -44,3 +48,7 @@ QStatusBar#PojectStatusBar QLabel {
margin: 0px;
padding: 0px 0px 0px 1px;
}

QPushButton#ModelicaEditorSearchButtons {

}
Binary file added OMEdit/OMEditGUI/Resources/icons/exit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OMEdit/OMEditGUI/Resources/icons/next.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OMEdit/OMEditGUI/Resources/icons/previous.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added OMEdit/OMEditGUI/Resources/icons/search.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4b8b3a4

Please sign in to comment.