Skip to content

Commit

Permalink
Allow block formats and text font.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Dec 20, 2016
1 parent 3f37fd0 commit 0770b88
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 43 deletions.
191 changes: 151 additions & 40 deletions OMEdit/OMEditGUI/Modeling/DocumentationWidget.cpp
Expand Up @@ -143,6 +143,26 @@ DocumentationWidget::DocumentationWidget(QWidget *pParent)
// create the html editor viewer
mpHTMLEditor = new DocumentationViewer(this, true);
// editor actions
// style combobox
mpStyleComboBox = new QComboBox;
mpStyleComboBox->setMinimumHeight(toolbarIconSize);
mpStyleComboBox->setToolTip(tr("Style"));
mpStyleComboBox->setStatusTip(tr("Sets the text style"));
mpStyleComboBox->addItem(tr("Normal"), "p");
mpStyleComboBox->addItem(tr("Heading 1"), "h1");
mpStyleComboBox->addItem(tr("Heading 2"), "h2");
mpStyleComboBox->addItem(tr("Heading 3"), "h3");
mpStyleComboBox->addItem(tr("Heading 4"), "h4");
mpStyleComboBox->addItem(tr("Heading 5"), "h5");
mpStyleComboBox->addItem(tr("Heading 6"), "h6");
mpStyleComboBox->addItem(tr("Preformatted"), "pre");
connect(mpStyleComboBox, SIGNAL(currentIndexChanged(int)), SLOT(formatBlock(int)));
// font combobox
mpFontComboBox = new QFontComboBox;
mpFontComboBox->setMinimumHeight(toolbarIconSize);
mpFontComboBox->setToolTip(tr("Font"));
mpFontComboBox->setStatusTip(tr("Sets the text font"));
connect(mpFontComboBox, SIGNAL(currentFontChanged(QFont)), SLOT(fontName(QFont)));
// bold action
mpBoldAction = new QAction(QIcon(":/Resources/icons/bold-icon.svg"), Helper::bold, this);
mpBoldAction->setStatusTip(tr("Make your text bold"));
Expand Down Expand Up @@ -262,6 +282,8 @@ DocumentationWidget::DocumentationWidget(QWidget *pParent)
pAlignmentButtonGroup->addButton(mpAlignRightToolButton);
pAlignmentButtonGroup->addButton(mpJustifyToolButton);
// add actions to toolbar
mpEditorToolBar->addWidget(mpStyleComboBox);
mpEditorToolBar->addWidget(mpFontComboBox);
mpEditorToolBar->addAction(mpBoldAction);
mpEditorToolBar->addAction(mpItalicAction);
mpEditorToolBar->addAction(mpUnderlineAction);
Expand Down Expand Up @@ -403,6 +425,63 @@ void DocumentationWidget::showDocumentation(LibraryTreeItem *pLibraryTreeItem)
mpEditorsWidget->hide();
}

/*!
* \brief DocumentationWidget::execCommand
* Calls the document.execCommand API.
* \param commandName
* \sa DocumentationWidget::execCommand(commandName, valueArgument)
*/
void DocumentationWidget::execCommand(const QString &commandName)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.execCommand(\"%1\", false, null)").arg(commandName);
pWebFrame->evaluateJavaScript(javaScript);
}

/*!
* \brief DocumentationWidget::execCommand
* Calls the document.execCommand API.
* \param command
* \param valueArgument
* \sa DocumentationWidget::execCommand(commandName)
*/
void DocumentationWidget::execCommand(const QString &command, const QString &valueArgument)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.execCommand(\"%1\", false, \"%2\")").arg(command).arg(valueArgument);
pWebFrame->evaluateJavaScript(javaScript);
}

/*!
* \brief DocumentationWidget::queryCommandState
* Calls the document.queryCommandState API.\n
* Returns true if the command is enabled e.g., if the text is bold then document.queryCommandState("bold", false, null) returns true.
* \param commandName
* \return
*/
bool DocumentationWidget::queryCommandState(const QString &commandName)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.queryCommandState(\"%1\", false, null)").arg(commandName);
QVariant result = pWebFrame->evaluateJavaScript(javaScript);
return result.toString().simplified().toLower() == "true";
}

/*!
* \brief DocumentationWidget::queryCommandValue
* Calls the document.queryCommandValue API.\n
* Returns the command value e.g., if the text is heading 1 then document.queryCommandValue("formatBlock", false, null) returns h1.
* \param commandName
* \return
*/
QString DocumentationWidget::queryCommandValue(const QString &commandName)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.queryCommandValue(\"%1\", false, null)").arg(commandName);
QVariant result = pWebFrame->evaluateJavaScript(javaScript);
return result.toString();
}

/*!
* \brief DocumentationWidget::createPixmapForToolButton
* Creates a new pixmap which contains the QIcon and the QColor in the bottom.
Expand Down Expand Up @@ -459,28 +538,6 @@ void DocumentationWidget::writeDocumentationFile(QString documentation)
mDocumentationFile.close();
}

void DocumentationWidget::execCommand(const QString &commandName)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.execCommand(\"%1\", false, null)").arg(commandName);
pWebFrame->evaluateJavaScript(javaScript);
}

void DocumentationWidget::execCommand(const QString &command, const QString &valueArgument)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.execCommand(\"%1\", false, \"%2\")").arg(command).arg(valueArgument);
pWebFrame->evaluateJavaScript(javaScript);
}

bool DocumentationWidget::queryCommandState(const QString &commandName)
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("document.queryCommandState(\"%1\", false, null)").arg(commandName);
QVariant result = pWebFrame->evaluateJavaScript(javaScript);
return result.toString().simplified().toLower() == "true";
}

/*!
* \brief DocumentationWidget::previousDocumentation
* Moves to the previous documentation.\n
Expand Down Expand Up @@ -537,7 +594,7 @@ void DocumentationWidget::editInfoDocumentation()
mpDocumentationViewer->hide();
mpEditorsWidget->show();
mpTabBar->setCurrentIndex(0);
mpHTMLEditor->setFocus(Qt::ActiveWindowFocusReason);
mpHTMLEditor->setFocusInternal();
}
}
}
Expand Down Expand Up @@ -572,7 +629,7 @@ void DocumentationWidget::editRevisionsDocumentation()
mpDocumentationViewer->hide();
mpEditorsWidget->show();
mpTabBar->setCurrentIndex(0);
mpHTMLEditor->setFocus(Qt::ActiveWindowFocusReason);
mpHTMLEditor->setFocusInternal();
}
}
}
Expand Down Expand Up @@ -607,7 +664,7 @@ void DocumentationWidget::editInfoHeaderDocumentation()
mpDocumentationViewer->hide();
mpEditorsWidget->show();
mpTabBar->setCurrentIndex(0);
mpHTMLEditor->setFocus(Qt::ActiveWindowFocusReason);
mpHTMLEditor->setFocusInternal();
}
}
}
Expand Down Expand Up @@ -730,7 +787,7 @@ void DocumentationWidget::toggleEditor(int tabIndex)
}
mpHTMLSourceEditor->hide();
mpHTMLEditorWidget->show();
mpHTMLEditor->setFocus(Qt::ActiveWindowFocusReason);
mpHTMLEditor->setFocusInternal();
break;
}
}
Expand All @@ -752,6 +809,40 @@ void DocumentationWidget::updateActions()
mpAlignCenterToolButton->setChecked(queryCommandState("justifyCenter"));
mpAlignRightToolButton->setChecked(queryCommandState("justifyRight"));
mpJustifyToolButton->setChecked(queryCommandState("justifyFull"));
bool state = mpStyleComboBox->blockSignals(true);
QString format = queryCommandValue("formatBlock");
int currentIndex = mpStyleComboBox->findData(format);
if (currentIndex > -1) {
mpStyleComboBox->setCurrentIndex(currentIndex);
} else {
mpStyleComboBox->setCurrentIndex(0);
}
mpStyleComboBox->blockSignals(state);
state = mpFontComboBox->blockSignals(true);
QString fontName = queryCommandValue("fontName");
// font name has extra single quote around it so remove it.
fontName = StringHandler::removeFirstLastSingleQuotes(fontName);
currentIndex = mpFontComboBox->findText(fontName, Qt::MatchExactly);
if (currentIndex > -1) {
mpFontComboBox->setCurrentIndex(currentIndex);
}
mpFontComboBox->blockSignals(state);
}

/*!
* \brief DocumentationWidget::formatBlock
* SLOT activated when style combobox is changed.
* \param index
*/
void DocumentationWidget::formatBlock(int index)
{
QString format = mpStyleComboBox->itemData(index).toString();
execCommand("formatBlock", format);
}

void DocumentationWidget::fontName(QFont font)
{
execCommand("fontName", font.family());
}

/*!
Expand Down Expand Up @@ -915,6 +1006,21 @@ DocumentationViewer::DocumentationViewer(DocumentationWidget *pDocumentationWidg
createActions();
}

/*!
* \brief DocumentationViewer::setFocusInternal
* Sets the focus on QWebView.\n
* QWebView need an initial mouse click to show the cursor.
*/
void DocumentationViewer::setFocusInternal()
{
setFocus(Qt::ActiveWindowFocusReason);
QPoint center = QPoint(0, 0);
QMouseEvent *pMouseEvent1 = new QMouseEvent(QEvent::MouseButtonPress, center, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QMouseEvent *pMouseEvent2 = new QMouseEvent(QEvent::MouseButtonRelease, center, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::postEvent(this, pMouseEvent1);
QApplication::postEvent(this, pMouseEvent2);
}

/*!
* \brief DocumentationViewer::createActions
*/
Expand Down Expand Up @@ -1059,24 +1165,29 @@ QWebView* DocumentationViewer::createWindow(QWebPage::WebWindowType type)
*/
void DocumentationViewer::keyPressEvent(QKeyEvent *event)
{
if (page()->isContentEditable()) {
QWebView::keyPressEvent(event);
return;
}
bool shiftModifier = event->modifiers().testFlag(Qt::ShiftModifier);
bool controlModifier = event->modifiers().testFlag(Qt::ControlModifier);
if (shiftModifier && !controlModifier && event->key() == Qt::Key_Backspace) {
if (mpDocumentationWidget->getNextToolButton()->isEnabled()) {
mpDocumentationWidget->nextDocumentation();
// if editable QWebView
if (page()->isContentEditable()) {
if (!shiftModifier && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
mpDocumentationWidget->execCommand("insertHTML", "<p><br></p>");
} else {
QWebView::keyPressEvent(event);
}
} else if (!shiftModifier && !controlModifier && event->key() == Qt::Key_Backspace) {
if (mpDocumentationWidget->getPreviousToolButton()->isEnabled()) {
mpDocumentationWidget->previousDocumentation();
} else { // if non-editable QWebView
if (shiftModifier && !controlModifier && event->key() == Qt::Key_Backspace) {
if (mpDocumentationWidget->getNextToolButton()->isEnabled()) {
mpDocumentationWidget->nextDocumentation();
}
} else if (!shiftModifier && !controlModifier && event->key() == Qt::Key_Backspace) {
if (mpDocumentationWidget->getPreviousToolButton()->isEnabled()) {
mpDocumentationWidget->previousDocumentation();
}
} else if (controlModifier && event->key() == Qt::Key_A) {
page()->triggerAction(QWebPage::SelectAll);
} else {
QWebView::keyPressEvent(event);
}
} else if (controlModifier && event->key() == Qt::Key_A) {
page()->triggerAction(QWebPage::SelectAll);
} else {
QWebView::keyPressEvent(event);
}
}

Expand Down
14 changes: 11 additions & 3 deletions OMEdit/OMEditGUI/Modeling/DocumentationWidget.h
Expand Up @@ -41,6 +41,8 @@
#include <QFile>
#include <QWebView>
#include <QToolBar>
#include <QComboBox>
#include <QFontComboBox>
#include <QColorDialog>

class LibraryTreeItem;
Expand Down Expand Up @@ -73,6 +75,10 @@ class DocumentationWidget : public QWidget
QToolButton* getNextToolButton() {return mpNextToolButton;}
DocumentationViewer* getDocumentationViewer() {return mpDocumentationViewer;}
void showDocumentation(LibraryTreeItem *pLibraryTreeItem);
void execCommand(const QString &commandName);
void execCommand(const QString &commandName, const QString &valueArgument);
bool queryCommandState(const QString &commandName);
QString queryCommandValue(const QString &commandName);
private:
QFile mDocumentationFile;
QToolButton *mpPreviousToolButton;
Expand All @@ -88,6 +94,8 @@ class DocumentationWidget : public QWidget
QWidget *mpHTMLEditorWidget;
QToolBar *mpEditorToolBar;
DocumentationViewer *mpHTMLEditor;
QComboBox *mpStyleComboBox;
QFontComboBox *mpFontComboBox;
QAction *mpBoldAction;
QAction *mpItalicAction;
QAction *mpUnderlineAction;
Expand All @@ -112,9 +120,6 @@ class DocumentationWidget : public QWidget
QPixmap createPixmapForToolButton(QColor color, QIcon icon);
void updatePreviousNextButtons();
void writeDocumentationFile(QString documentation);
void execCommand(const QString &commandName);
void execCommand(const QString &commandName, const QString &valueArgument);
bool queryCommandState(const QString &commandName);
public slots:
void previousDocumentation();
void nextDocumentation();
Expand All @@ -125,6 +130,8 @@ public slots:
void cancelDocumentation();
void toggleEditor(int tabIndex);
void updateActions();
void formatBlock(int index);
void fontName(QFont font);
void applyTextColor();
void applyTextColor(QColor color);
void applyBackgroundColor();
Expand All @@ -144,6 +151,7 @@ class DocumentationViewer : public QWebView
DocumentationWidget *mpDocumentationWidget;
public:
DocumentationViewer(DocumentationWidget *pDocumentationWidget, bool isContentEditable = false);
void setFocusInternal();
private:
void createActions();
void resetZoom();
Expand Down
15 changes: 15 additions & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.cpp
Expand Up @@ -734,6 +734,21 @@ QString StringHandler::removeFirstLastQuotes(QString value)
return value;
}

/*!
* \brief StringHandler::removeFirstLastSingleQuotes
* Removes the first and last single quotes from the string.
* \param value
* \return
*/
QString StringHandler::removeFirstLastSingleQuotes(QString value)
{
value = value.trimmed();
if (value.length() > 1 && value.at(0) == '\'' && value.at(value.length() - 1) == '\'') {
value = value.mid(1, (value.length() - 2));
}
return value;
}

QStringList StringHandler::getStrings(QString value)
{
return getStrings(value, '{', '}');
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.h
Expand Up @@ -105,6 +105,7 @@ class StringHandler : public QObject
static QString removeFirstLastCurlBrackets(QString value);
static QString removeFirstLastBrackets(QString value);
static QString removeFirstLastQuotes(QString value);
static QString removeFirstLastSingleQuotes(QString value);
static QStringList getStrings(QString value);
static QStringList getStrings(QString value, char start, char end);
/* Handles quoted identifiers A.B.'C.D' -> A.B, A.B.C.D -> A.B.C */
Expand Down

0 comments on commit 0770b88

Please sign in to comment.