diff options
author | Aurindam Jana <aurindam.jana@digia.com> | 2012-10-04 14:54:59 +0200 |
---|---|---|
committer | hjk <qthjk@ovi.com> | 2012-10-05 11:03:01 +0200 |
commit | 9ac25a293252a1196dad29b493add514459777a6 (patch) | |
tree | 12b2e4aa58a7f310d0596bd067f458356ad58dea /src/plugins/qmljstools/qmlconsoleview.cpp | |
parent | 614bb33589aa1fade05cd1b6b32b3acc848f2fd8 (diff) | |
download | qt-creator-9ac25a293252a1196dad29b493add514459777a6.tar.gz |
ScriptConsole: Move from Debugger to QmlJSTools
The console is now a part of qmljstools plugin. The console
appears as an output pane. A dummy QScriptEngine evaluates
expressions when a declarative debug session is not in
progress. During a debug session, the expressions are
evaluated by the debug services.
Task-Number: QTCREATORBUG-7402
Change-Id: Ic2eeac44fb335c706be03b89f8672b0356efe984
Reviewed-by: Christiaan Janssen <christiaan.janssen@digia.com>
Reviewed-by: hjk <qthjk@ovi.com>
Diffstat (limited to 'src/plugins/qmljstools/qmlconsoleview.cpp')
-rw-r--r-- | src/plugins/qmljstools/qmlconsoleview.cpp | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/plugins/qmljstools/qmlconsoleview.cpp b/src/plugins/qmljstools/qmlconsoleview.cpp new file mode 100644 index 0000000000..3599cabdb7 --- /dev/null +++ b/src/plugins/qmljstools/qmlconsoleview.cpp @@ -0,0 +1,221 @@ +#include "qmlconsoleview.h" +#include "qmlconsoleitemdelegate.h" +#include "qmlconsoleitemmodel.h" + +#include <texteditor/basetexteditor.h> + +#include <QMouseEvent> +#include <QProxyStyle> +#include <QPainter> +#include <QApplication> +#include <QClipboard> +#include <QAbstractProxyModel> +#include <QFileInfo> +#include <QUrl> +#include <QScrollBar> + +namespace QmlJSTools { +namespace Internal { + +class QmlConsoleViewStyle : public QProxyStyle +{ +public: + void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, + const QWidget *widget = 0) const + { + if (element != QStyle::PE_PanelItemViewRow) + QProxyStyle::drawPrimitive(element, option, painter, widget); + } + + int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, + QStyleHintReturn *returnData = 0) const { + if (hint == SH_ItemView_ShowDecorationSelected) + return 0; + else + return QProxyStyle::styleHint(hint, option, widget, returnData); + } +}; + +/////////////////////////////////////////////////////////////////////// +// +// QmlConsoleView +// +/////////////////////////////////////////////////////////////////////// + +QmlConsoleView::QmlConsoleView(QWidget *parent) : + QTreeView(parent) +{ + setFrameStyle(QFrame::NoFrame); + setHeaderHidden(true); + setRootIsDecorated(false); + setEditTriggers(QAbstractItemView::AllEditTriggers); + setStyleSheet(QLatin1String("QTreeView::branch:has-siblings:!adjoins-item {" + "border-image: none;" + "image: none; }" + "QTreeView::branch:has-siblings:adjoins-item {" + "border-image: none;" + "image: none; }" + "QTreeView::branch:!has-children:!has-siblings:adjoins-item {" + "border-image: none;" + "image: none; }" + "QTreeView::branch:has-children:!has-siblings:closed," + "QTreeView::branch:closed:has-children:has-siblings {" + "border-image: none;" + "image: none; }" + "QTreeView::branch:open:has-children:!has-siblings," + "QTreeView::branch:open:has-children:has-siblings {" + "border-image: none;" + "image: none; }")); + QmlConsoleViewStyle *style = new QmlConsoleViewStyle; + setStyle(style); + style->setParent(this); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + + connect(this, SIGNAL(activated(QModelIndex)), SLOT(onRowActivated(QModelIndex))); +} + +void QmlConsoleView::onScrollToBottom() +{ + // Keep scrolling to bottom if scroll bar is at maximum() + if (verticalScrollBar()->value() == verticalScrollBar()->maximum()) + scrollToBottom(); +} + +void QmlConsoleView::mousePressEvent(QMouseEvent *event) +{ + QPoint pos = event->pos(); + QModelIndex index = indexAt(pos); + if (index.isValid()) { + QmlConsoleItem::ItemType type = (QmlConsoleItem::ItemType)index.data( + QmlConsoleItemModel::TypeRole).toInt(); + bool handled = false; + if (type == QmlConsoleItem::UndefinedType) { + bool showTypeIcon = index.parent() == QModelIndex(); + ConsoleItemPositions positions(visualRect(index), viewOptions().font, showTypeIcon, + true); + + if (positions.expandCollapseIcon().contains(pos)) { + if (isExpanded(index)) + setExpanded(index, false); + else + setExpanded(index, true); + handled = true; + } + } + if (!handled) + QTreeView::mousePressEvent(event); + } else { + selectionModel()->setCurrentIndex(model()->index(model()->rowCount() - 1, 0), + QItemSelectionModel::ClearAndSelect); + } +} + +void QmlConsoleView::keyPressEvent(QKeyEvent *e) +{ + if (!e->modifiers() && e->key() == Qt::Key_Return) { + emit activated(currentIndex()); + e->accept(); + return; + } + QTreeView::keyPressEvent(e); +} + +void QmlConsoleView::resizeEvent(QResizeEvent *e) +{ + static_cast<QmlConsoleItemDelegate *>(itemDelegate())->emitSizeHintChanged( + selectionModel()->currentIndex()); + QTreeView::resizeEvent(e); +} + +void QmlConsoleView::drawBranches(QPainter *painter, const QRect &rect, + const QModelIndex &index) const +{ + static_cast<QmlConsoleItemDelegate *>(itemDelegate())->drawBackground(painter, rect, index, + false); + QTreeView::drawBranches(painter, rect, index); +} + +void QmlConsoleView::contextMenuEvent(QContextMenuEvent *event) +{ + QModelIndex itemIndex = indexAt(event->pos()); + QMenu menu; + + QAction *copy = new QAction(tr("&Copy"), this); + copy->setEnabled(itemIndex.isValid()); + menu.addAction(copy); + QAction *show = new QAction(tr("&Show in Editor"), this); + show->setEnabled(canShowItemInTextEditor(itemIndex)); + menu.addAction(show); + menu.addSeparator(); + QAction *clear = new QAction(tr("C&lear"), this); + menu.addAction(clear); + + QAction *a = menu.exec(event->globalPos()); + if (a == 0) + return; + + if (a == copy) { + copyToClipboard(itemIndex); + } else if (a == show) { + onRowActivated(itemIndex); + } else if (a == clear) { + QAbstractProxyModel *proxyModel = qobject_cast<QAbstractProxyModel *>(model()); + QmlConsoleItemModel *handler = qobject_cast<QmlConsoleItemModel *>( + proxyModel->sourceModel()); + handler->clear(); + } +} + +void QmlConsoleView::onRowActivated(const QModelIndex &index) +{ + if (!index.isValid()) + return; + + // See if we have file and line Info + QString filePath = model()->data(index, + QmlConsoleItemModel::FileRole).toString(); + if (!filePath.isEmpty()) { + QFileInfo fi(filePath); + if (fi.exists() && fi.isFile() && fi.isReadable()) { + int line = model()->data(index, QmlConsoleItemModel::LineRole).toInt(); + TextEditor::BaseTextEditorWidget::openEditorAt(fi.canonicalFilePath(), line); + } + } +} + +void QmlConsoleView::copyToClipboard(const QModelIndex &index) +{ + if (!index.isValid()) + return; + + QString contents = model()->data(index).toString(); + // See if we have file and line Info + QString filePath = model()->data(index, QmlConsoleItemModel::FileRole).toString(); + if (!filePath.isEmpty()) { + contents = QString(QLatin1String("%1 %2: %3")).arg(contents).arg(filePath).arg( + model()->data(index, QmlConsoleItemModel::LineRole).toString()); + } + QClipboard *cb = QApplication::clipboard(); + cb->setText(contents); +} + +bool QmlConsoleView::canShowItemInTextEditor(const QModelIndex &index) +{ + if (!index.isValid()) + return false; + + // See if we have file and line Info + QString filePath = model()->data(index, QmlConsoleItemModel::FileRole).toString(); + if (!filePath.isEmpty()) { + QFileInfo fi(filePath); + if (fi.exists() && fi.isFile() && fi.isReadable()) { + return true; + } + } + return false; +} + +} // Internal +} // QmlJSTools |