2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2012-10-05 11:50:16 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2012-10-05 11:50:16 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2012-10-05 11:50:16 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2012-10-05 11:50:16 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2012-10-05 11:50:16 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2012-10-05 11:50:16 +02:00
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
#include "qmlconsoleview.h"
|
|
|
|
|
#include "qmlconsoleitemdelegate.h"
|
|
|
|
|
#include "qmlconsoleitemmodel.h"
|
2012-02-22 11:50:05 +01:00
|
|
|
|
2013-05-30 17:26:51 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2012-11-21 14:00:41 +01:00
|
|
|
#include <coreplugin/manhattanstyle.h>
|
2013-03-14 12:20:43 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
2012-02-08 15:54:59 +01:00
|
|
|
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QAction>
|
|
|
|
|
#include <QMenu>
|
2012-02-17 22:33:40 +02:00
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QPainter>
|
2012-02-22 11:50:05 +01:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QClipboard>
|
|
|
|
|
#include <QAbstractProxyModel>
|
|
|
|
|
#include <QFileInfo>
|
2012-02-27 11:25:30 +01:00
|
|
|
#include <QScrollBar>
|
2012-11-21 14:00:41 +01:00
|
|
|
#include <QStyleFactory>
|
|
|
|
|
#include <QString>
|
2013-06-05 12:11:08 +02:00
|
|
|
#include <QUrl>
|
2012-02-08 15:54:59 +01:00
|
|
|
|
2012-10-08 13:17:10 +02:00
|
|
|
using namespace QmlJS;
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
namespace QmlJSTools {
|
2012-02-08 15:54:59 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2012-11-21 14:00:41 +01:00
|
|
|
class QmlConsoleViewStyle : public ManhattanStyle
|
2012-02-08 15:54:59 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2012-11-21 14:00:41 +01:00
|
|
|
QmlConsoleViewStyle(const QString &baseStyleName) : ManhattanStyle(baseStyleName) {}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter,
|
2012-02-08 15:54:59 +01:00
|
|
|
const QWidget *widget = 0) const
|
|
|
|
|
{
|
|
|
|
|
if (element != QStyle::PE_PanelItemViewRow)
|
2012-11-21 14:00:41 +01:00
|
|
|
ManhattanStyle::drawPrimitive(element, option, painter, widget);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0,
|
2012-02-08 15:54:59 +01:00
|
|
|
QStyleHintReturn *returnData = 0) const {
|
|
|
|
|
if (hint == SH_ItemView_ShowDecorationSelected)
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
2012-11-21 14:00:41 +01:00
|
|
|
return ManhattanStyle::styleHint(hint, option, widget, returnData);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2012-10-04 14:54:59 +02:00
|
|
|
// QmlConsoleView
|
2012-02-08 15:54:59 +01:00
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
QmlConsoleView::QmlConsoleView(QWidget *parent) :
|
2014-04-09 18:22:54 +02:00
|
|
|
Utils::TreeView(parent)
|
2012-02-08 15:54:59 +01:00
|
|
|
{
|
|
|
|
|
setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
setHeaderHidden(true);
|
|
|
|
|
setRootIsDecorated(false);
|
2013-11-08 15:50:49 +01:00
|
|
|
setUniformRowHeights(true);
|
2012-02-08 15:54:59 +01:00
|
|
|
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; }"));
|
2012-11-21 14:00:41 +01:00
|
|
|
|
|
|
|
|
QString baseName = QApplication::style()->objectName();
|
2013-03-14 12:20:43 +01:00
|
|
|
if (Utils::HostOsInfo::isAnyUnixHost() && !Utils::HostOsInfo::isMacHost()
|
|
|
|
|
&& baseName == QLatin1String("windows")) {
|
2012-11-21 14:00:41 +01:00
|
|
|
// Sometimes we get the standard windows 95 style as a fallback
|
2013-07-17 00:01:45 +03:00
|
|
|
if (QStyleFactory::keys().contains(QLatin1String("Fusion"))) {
|
2012-11-21 14:00:41 +01:00
|
|
|
baseName = QLatin1String("fusion"); // Qt5
|
2013-07-17 00:01:45 +03:00
|
|
|
} else { // Qt4
|
2012-11-21 14:00:41 +01:00
|
|
|
// e.g. if we are running on a KDE4 desktop
|
|
|
|
|
QByteArray desktopEnvironment = qgetenv("DESKTOP_SESSION");
|
|
|
|
|
if (desktopEnvironment == "kde")
|
|
|
|
|
baseName = QLatin1String("plastique");
|
|
|
|
|
else
|
|
|
|
|
baseName = QLatin1String("cleanlooks");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QmlConsoleViewStyle *style = new QmlConsoleViewStyle(baseName);
|
2012-02-08 15:54:59 +01:00
|
|
|
setStyle(style);
|
|
|
|
|
style->setParent(this);
|
2012-02-22 11:50:05 +01:00
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
|
|
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
2013-10-24 17:57:29 +02:00
|
|
|
horizontalScrollBar()->setSingleStep(20);
|
|
|
|
|
verticalScrollBar()->setSingleStep(20);
|
2012-02-22 11:50:05 +01:00
|
|
|
|
2015-01-30 11:02:24 +01:00
|
|
|
connect(this, &QmlConsoleView::activated, this, &QmlConsoleView::onRowActivated);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::onScrollToBottom()
|
2012-02-27 11:25:30 +01:00
|
|
|
{
|
2014-02-18 14:56:19 +01:00
|
|
|
// Keep scrolling to bottom if scroll bar is not at maximum()
|
|
|
|
|
if (verticalScrollBar()->value() != verticalScrollBar()->maximum())
|
2012-02-27 11:25:30 +01:00
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::mousePressEvent(QMouseEvent *event)
|
2012-02-08 15:54:59 +01:00
|
|
|
{
|
|
|
|
|
QPoint pos = event->pos();
|
|
|
|
|
QModelIndex index = indexAt(pos);
|
|
|
|
|
if (index.isValid()) {
|
2012-10-08 13:17:10 +02:00
|
|
|
ConsoleItem::ItemType type = (ConsoleItem::ItemType)index.data(
|
2012-10-04 14:54:59 +02:00
|
|
|
QmlConsoleItemModel::TypeRole).toInt();
|
2012-02-22 11:50:05 +01:00
|
|
|
bool handled = false;
|
2012-10-08 13:17:10 +02:00
|
|
|
if (type == ConsoleItem::UndefinedType) {
|
2012-02-22 11:50:05 +01:00
|
|
|
bool showTypeIcon = index.parent() == QModelIndex();
|
2012-10-04 14:54:59 +02:00
|
|
|
ConsoleItemPositions positions(visualRect(index), viewOptions().font, showTypeIcon,
|
|
|
|
|
true);
|
2012-02-22 11:50:05 +01:00
|
|
|
|
|
|
|
|
if (positions.expandCollapseIcon().contains(pos)) {
|
|
|
|
|
if (isExpanded(index))
|
|
|
|
|
setExpanded(index, false);
|
|
|
|
|
else
|
|
|
|
|
setExpanded(index, true);
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
2012-02-22 11:50:05 +01:00
|
|
|
if (!handled)
|
2014-04-09 18:22:54 +02:00
|
|
|
Utils::TreeView::mousePressEvent(event);
|
2012-02-08 15:54:59 +01:00
|
|
|
} else {
|
2012-10-04 14:54:59 +02:00
|
|
|
selectionModel()->setCurrentIndex(model()->index(model()->rowCount() - 1, 0),
|
|
|
|
|
QItemSelectionModel::ClearAndSelect);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::resizeEvent(QResizeEvent *e)
|
2012-02-08 15:54:59 +01:00
|
|
|
{
|
2012-10-04 14:54:59 +02:00
|
|
|
static_cast<QmlConsoleItemDelegate *>(itemDelegate())->emitSizeHintChanged(
|
2012-02-08 15:54:59 +01:00
|
|
|
selectionModel()->currentIndex());
|
2014-04-09 18:22:54 +02:00
|
|
|
Utils::TreeView::resizeEvent(e);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::drawBranches(QPainter *painter, const QRect &rect,
|
|
|
|
|
const QModelIndex &index) const
|
2012-02-08 15:54:59 +01:00
|
|
|
{
|
2012-10-04 14:54:59 +02:00
|
|
|
static_cast<QmlConsoleItemDelegate *>(itemDelegate())->drawBackground(painter, rect, index,
|
|
|
|
|
false);
|
2014-04-09 18:22:54 +02:00
|
|
|
Utils::TreeView::drawBranches(painter, rect, index);
|
2012-02-08 15:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::contextMenuEvent(QContextMenuEvent *event)
|
2012-02-22 11:50:05 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
if (a == copy) {
|
2012-02-22 11:50:05 +01:00
|
|
|
copyToClipboard(itemIndex);
|
2012-10-04 14:54:59 +02:00
|
|
|
} else if (a == show) {
|
2012-02-22 11:50:05 +01:00
|
|
|
onRowActivated(itemIndex);
|
2012-10-04 14:54:59 +02:00
|
|
|
} else if (a == clear) {
|
|
|
|
|
QAbstractProxyModel *proxyModel = qobject_cast<QAbstractProxyModel *>(model());
|
|
|
|
|
QmlConsoleItemModel *handler = qobject_cast<QmlConsoleItemModel *>(
|
|
|
|
|
proxyModel->sourceModel());
|
2012-02-22 11:50:05 +01:00
|
|
|
handler->clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::onRowActivated(const QModelIndex &index)
|
2012-02-22 11:50:05 +01:00
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
// See if we have file and line Info
|
2013-05-24 16:31:01 +02:00
|
|
|
QString filePath = model()->data(index, QmlConsoleItemModel::FileRole).toString();
|
|
|
|
|
const QUrl fileUrl = QUrl(filePath);
|
|
|
|
|
if (fileUrl.isLocalFile())
|
|
|
|
|
filePath = fileUrl.toLocalFile();
|
2012-02-22 11:50:05 +01:00
|
|
|
if (!filePath.isEmpty()) {
|
|
|
|
|
QFileInfo fi(filePath);
|
|
|
|
|
if (fi.exists() && fi.isFile() && fi.isReadable()) {
|
2012-10-04 14:54:59 +02:00
|
|
|
int line = model()->data(index, QmlConsoleItemModel::LineRole).toInt();
|
2013-05-30 17:26:51 +02:00
|
|
|
Core::EditorManager::openEditorAt(fi.canonicalFilePath(), line);
|
2012-02-22 11:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
void QmlConsoleView::copyToClipboard(const QModelIndex &index)
|
2012-02-22 11:50:05 +01:00
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-03-05 10:50:26 +01:00
|
|
|
QString contents = model()->data(index, QmlConsoleItemModel::ExpressionRole).toString();
|
2012-10-04 14:54:59 +02:00
|
|
|
// See if we have file and line Info
|
|
|
|
|
QString filePath = model()->data(index, QmlConsoleItemModel::FileRole).toString();
|
2013-05-24 16:31:01 +02:00
|
|
|
const QUrl fileUrl = QUrl(filePath);
|
|
|
|
|
if (fileUrl.isLocalFile())
|
|
|
|
|
filePath = fileUrl.toLocalFile();
|
2012-02-22 11:50:05 +01:00
|
|
|
if (!filePath.isEmpty()) {
|
2014-05-08 22:12:53 +02:00
|
|
|
contents = QString::fromLatin1("%1 %2: %3").arg(contents).arg(filePath).arg(
|
2012-10-04 14:54:59 +02:00
|
|
|
model()->data(index, QmlConsoleItemModel::LineRole).toString());
|
2012-02-22 11:50:05 +01:00
|
|
|
}
|
|
|
|
|
QClipboard *cb = QApplication::clipboard();
|
|
|
|
|
cb->setText(contents);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
bool QmlConsoleView::canShowItemInTextEditor(const QModelIndex &index)
|
2012-02-22 11:50:05 +01:00
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
// See if we have file and line Info
|
|
|
|
|
QString filePath = model()->data(index, QmlConsoleItemModel::FileRole).toString();
|
2013-05-24 16:31:01 +02:00
|
|
|
const QUrl fileUrl = QUrl(filePath);
|
|
|
|
|
if (fileUrl.isLocalFile())
|
|
|
|
|
filePath = fileUrl.toLocalFile();
|
2012-02-22 11:50:05 +01:00
|
|
|
if (!filePath.isEmpty()) {
|
|
|
|
|
QFileInfo fi(filePath);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (fi.exists() && fi.isFile() && fi.isReadable())
|
2012-02-22 11:50:05 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 14:54:59 +02:00
|
|
|
} // Internal
|
|
|
|
|
} // QmlJSTools
|