Skip to content

Commit

Permalink
Allow creating link and unlinking it.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Dec 20, 2016
1 parent 0be5dc2 commit c93c603
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
47 changes: 39 additions & 8 deletions OMEdit/OMEditGUI/Modeling/DocumentationWidget.cpp
Expand Up @@ -51,6 +51,7 @@
#include <QWebFrame>
#include <QWidgetAction>
#include <QButtonGroup>
#include <QInputDialog>

/*!
* \class DocumentationWidget
Expand Down Expand Up @@ -342,9 +343,9 @@ DocumentationWidget::DocumentationWidget(QWidget *pParent)
mpEditorToolBar->addSeparator();
mpEditorToolBar->addAction(mpBulletListAction);
mpEditorToolBar->addAction(mpNumberedListAction);
// mpEditorToolBar->addSeparator();
// mpEditorToolBar->addAction(mpLinkAction);
// mpEditorToolBar->addAction(mpUnLinkAction);
mpEditorToolBar->addSeparator();
mpEditorToolBar->addAction(mpLinkAction);
mpEditorToolBar->addAction(mpUnLinkAction);
// update the actions whenever the selectionChanged signal is raised.
connect(mpHTMLEditor->page(), SIGNAL(selectionChanged()), SLOT(updateActions()));
// add a layout to html editor widget
Expand Down Expand Up @@ -585,6 +586,26 @@ void DocumentationWidget::writeDocumentationFile(QString documentation)
mDocumentationFile.close();
}

/*!
* \brief DocumentationWidget::isLinkSelected
* Returns true if a link is selected.
* \return
*/
bool DocumentationWidget::isLinkSelected()
{
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("function isLinkSelected() {"
" if (document.getSelection().anchorNode.parentNode.nodeName == 'A') {"
" return true;"
" } else {"
" return false;"
" }"
"}"
"isLinkSelected()");
QVariant result = pWebFrame->evaluateJavaScript(javaScript);
return result.toString().simplified().toLower() == "true";
}

/*!
* \brief DocumentationWidget::previousDocumentation
* Moves to the previous documentation.\n
Expand Down Expand Up @@ -883,8 +904,8 @@ void DocumentationWidget::updateActions()
mpJustifyToolButton->setChecked(queryCommandState("justifyFull"));
mpBulletListAction->setChecked(queryCommandState("insertUnorderedList"));
mpNumberedListAction->setChecked(queryCommandState("insertOrderedList"));
// mpLinkAction->setEnabled(mpHTMLEditor->page()->hasSelection());
// mpUnLinkAction->setEnabled(mpHTMLEditor->page()->hasSelection() && queryCommandState("unlink"));
mpLinkAction->setEnabled(!mpHTMLEditor->page()->selectedText().isEmpty());
mpUnLinkAction->setEnabled(!mpHTMLEditor->page()->selectedText().isEmpty() && isLinkSelected());
}

/*!
Expand Down Expand Up @@ -1028,9 +1049,19 @@ void DocumentationWidget::numberedList()
*/
void DocumentationWidget::createLink()
{
QString link = queryCommandValue("createLink");
qDebug() << link;
execCommand("createLink", "http://www.google.com");
QWebFrame *pWebFrame = mpHTMLEditor->page()->mainFrame();
QString javaScript = QString("function getLinkHref() {"
" if (document.getSelection().anchorNode.parentNode.nodeName == 'A') {"
" if (document.getSelection().anchorNode.parentNode.hasAttribute('href')) {"
" return document.getSelection().anchorNode.parentNode.getAttribute('href');"
" }"
" }"
" return '';"
"}"
"getLinkHref()");
QString href = pWebFrame->evaluateJavaScript(javaScript).toString();
href = QInputDialog::getText(this, tr("Create Link"), "Enter URL", QLineEdit::Normal, href);
execCommand("createLink", href);
}

/*!
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Modeling/DocumentationWidget.h
Expand Up @@ -128,6 +128,7 @@ class DocumentationWidget : public QWidget
QPixmap createPixmapForToolButton(QColor color, QIcon icon);
void updatePreviousNextButtons();
void writeDocumentationFile(QString documentation);
bool isLinkSelected();
public slots:
void previousDocumentation();
void nextDocumentation();
Expand Down

0 comments on commit c93c603

Please sign in to comment.