2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-10 20:58:17 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-02-10 20:58:17 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-02-10 20:58:17 +01: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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-02-10 20:58:17 +01: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
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-02-10 20:58:17 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-02-10 20:58:17 +01:00
|
|
|
|
|
|
|
|
#include "variablechooser.h"
|
2011-03-03 20:50:25 +01:00
|
|
|
#include "coreconstants.h"
|
2011-02-10 20:58:17 +01:00
|
|
|
|
2011-08-17 12:54:58 +02:00
|
|
|
#include <utils/fancylineedit.h> // IconButton
|
2014-10-20 23:13:13 +02:00
|
|
|
#include <utils/headerviewstretcher.h> // IconButton
|
2014-10-13 18:49:44 +02:00
|
|
|
#include <utils/macroexpander.h>
|
|
|
|
|
#include <utils/treemodel.h>
|
2013-03-28 11:05:35 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2011-08-17 12:54:58 +02:00
|
|
|
|
2014-06-20 00:34:41 +02:00
|
|
|
#include <QApplication>
|
2014-10-13 18:49:44 +02:00
|
|
|
#include <QAbstractItemModel>
|
|
|
|
|
#include <QHeaderView>
|
2014-06-20 00:34:41 +02:00
|
|
|
#include <QLabel>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QListWidgetItem>
|
2014-10-20 23:13:13 +02:00
|
|
|
#include <QMenu>
|
2014-06-20 00:34:41 +02:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QPointer>
|
|
|
|
|
#include <QTextEdit>
|
|
|
|
|
#include <QTimer>
|
2014-10-13 18:49:44 +02:00
|
|
|
#include <QTreeView>
|
2014-06-20 00:34:41 +02:00
|
|
|
#include <QVBoxLayout>
|
2014-10-13 18:49:44 +02:00
|
|
|
#include <QVector>
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
2011-06-22 16:21:04 +02:00
|
|
|
|
2014-06-20 00:34:41 +02:00
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
enum {
|
|
|
|
|
UnexpandedTextRole = Qt::UserRole,
|
|
|
|
|
ExpandedTextRole
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VariableTreeView : public QTreeView
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VariableTreeView(QWidget *parent, VariableChooserPrivate *target)
|
|
|
|
|
: QTreeView(parent), m_target(target)
|
|
|
|
|
{
|
|
|
|
|
setAttribute(Qt::WA_MacSmallSize);
|
|
|
|
|
setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
|
|
|
setIndentation(indentation() * 7/10);
|
|
|
|
|
header()->hide();
|
|
|
|
|
new Utils::HeaderViewStretcher(header(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void contextMenuEvent(QContextMenuEvent *ev);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
VariableChooserPrivate *m_target;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-06-20 00:34:41 +02:00
|
|
|
class VariableChooserPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-10-13 18:49:44 +02:00
|
|
|
VariableChooserPrivate(VariableChooser *parent);
|
2014-06-20 00:34:41 +02:00
|
|
|
|
|
|
|
|
void createIconButton()
|
|
|
|
|
{
|
|
|
|
|
m_iconButton = new Utils::IconButton;
|
|
|
|
|
m_iconButton->setPixmap(QPixmap(QLatin1String(":/core/images/replace.png")));
|
|
|
|
|
m_iconButton->setToolTip(tr("Insert variable"));
|
|
|
|
|
m_iconButton->hide();
|
2014-10-13 18:49:44 +02:00
|
|
|
connect(m_iconButton.data(), static_cast<void(QAbstractButton::*)(bool)>(&QAbstractButton::clicked),
|
|
|
|
|
this, &VariableChooserPrivate::updatePositionAndShow);
|
2014-06-20 00:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
void updateDescription(const QModelIndex &index);
|
2014-06-20 00:34:41 +02:00
|
|
|
void updateCurrentEditor(QWidget *old, QWidget *widget);
|
2014-10-13 18:49:44 +02:00
|
|
|
void handleItemActivated(const QModelIndex &index);
|
2014-10-20 23:13:13 +02:00
|
|
|
void insertText(const QString &variable);
|
2014-10-13 18:49:44 +02:00
|
|
|
void updatePositionAndShow(bool);
|
2014-06-20 00:34:41 +02:00
|
|
|
|
|
|
|
|
QWidget *currentWidget();
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
public:
|
2014-06-20 00:34:41 +02:00
|
|
|
VariableChooser *q;
|
2014-10-13 18:49:44 +02:00
|
|
|
TreeModel m_model;
|
|
|
|
|
|
2014-06-20 00:34:41 +02:00
|
|
|
QPointer<QLineEdit> m_lineEdit;
|
|
|
|
|
QPointer<QTextEdit> m_textEdit;
|
|
|
|
|
QPointer<QPlainTextEdit> m_plainTextEdit;
|
|
|
|
|
QPointer<Utils::IconButton> m_iconButton;
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
VariableTreeView *m_variableTree;
|
2014-06-20 00:34:41 +02:00
|
|
|
QLabel *m_variableDescription;
|
2014-10-13 18:49:44 +02:00
|
|
|
QString m_defaultDescription;
|
|
|
|
|
QByteArray m_currentVariableName; // Prevent recursive insertion of currently expanded item
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VariableGroupItem : public TreeItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-10-20 23:13:13 +02:00
|
|
|
VariableGroupItem()
|
|
|
|
|
: m_chooser(0), m_expander(0)
|
2014-10-13 18:49:44 +02:00
|
|
|
{
|
|
|
|
|
setLazy(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ensureExpander() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_expander)
|
|
|
|
|
m_expander = m_provider();
|
|
|
|
|
return m_expander != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant data(int column, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
|
if (column == 0 && ensureExpander())
|
|
|
|
|
return m_expander->displayName();
|
|
|
|
|
}
|
2014-10-20 23:13:13 +02:00
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void populate()
|
|
|
|
|
{
|
2014-10-20 23:13:13 +02:00
|
|
|
if (ensureExpander())
|
|
|
|
|
populateGroup(m_expander);
|
2014-10-13 18:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
void populateGroup(MacroExpander *expander);
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
public:
|
|
|
|
|
VariableChooserPrivate *m_chooser; // Not owned.
|
|
|
|
|
MacroExpanderProvider m_provider;
|
|
|
|
|
mutable MacroExpander *m_expander; // Not owned.
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
class VariableItem : public TreeItem
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VariableItem() {}
|
|
|
|
|
|
|
|
|
|
QVariant data(int column, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
|
if (column == 0)
|
|
|
|
|
return m_variable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (role == Qt::ToolTipRole)
|
|
|
|
|
return m_expander->variableDescription(m_variable.toUtf8());
|
|
|
|
|
|
|
|
|
|
if (role == UnexpandedTextRole)
|
|
|
|
|
return QString(QLatin1String("%{") + m_variable + QLatin1Char('}'));
|
|
|
|
|
|
|
|
|
|
if (role == ExpandedTextRole)
|
|
|
|
|
return m_expander->expand(QLatin1String("%{") + m_variable + QLatin1Char('}'));
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
MacroExpander *m_expander;
|
|
|
|
|
QString m_variable;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void VariableTreeView::contextMenuEvent(QContextMenuEvent *ev)
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex index = indexAt(ev->pos());
|
|
|
|
|
|
|
|
|
|
QString unexpandedText = index.data(UnexpandedTextRole).toString();
|
|
|
|
|
QString expandedText = index.data(ExpandedTextRole).toString();
|
|
|
|
|
|
|
|
|
|
QMenu menu;
|
|
|
|
|
QAction *insertUnexpandedAction = 0;
|
|
|
|
|
QAction *insertExpandedAction = 0;
|
|
|
|
|
|
|
|
|
|
if (unexpandedText.isEmpty()) {
|
|
|
|
|
insertUnexpandedAction = menu.addAction(tr("Insert unexpanded value"));
|
|
|
|
|
insertUnexpandedAction->setEnabled(false);
|
|
|
|
|
} else {
|
|
|
|
|
insertUnexpandedAction = menu.addAction(tr("Insert \"%1\"").arg(unexpandedText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expandedText.isEmpty()) {
|
|
|
|
|
insertExpandedAction = menu.addAction(tr("Insert expanded value"));
|
|
|
|
|
insertExpandedAction->setEnabled(false);
|
|
|
|
|
} else {
|
|
|
|
|
insertExpandedAction = menu.addAction(tr("Insert \"%1\"").arg(expandedText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QAction *act = menu.exec(ev->globalPos());
|
|
|
|
|
|
|
|
|
|
if (act == insertUnexpandedAction)
|
|
|
|
|
m_target->insertText(unexpandedText);
|
|
|
|
|
else if (act == insertExpandedAction)
|
|
|
|
|
m_target->insertText(expandedText);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
|
|
|
|
|
: q(parent),
|
|
|
|
|
m_lineEdit(0),
|
|
|
|
|
m_textEdit(0),
|
|
|
|
|
m_plainTextEdit(0)
|
|
|
|
|
{
|
|
|
|
|
m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
m_variableTree = new VariableTreeView(q, this);
|
2014-10-13 18:49:44 +02:00
|
|
|
m_variableTree->setModel(&m_model);
|
|
|
|
|
|
|
|
|
|
m_variableDescription = new QLabel(q);
|
|
|
|
|
m_variableDescription->setText(m_defaultDescription);
|
|
|
|
|
m_variableDescription->setMinimumSize(QSize(0, 60));
|
|
|
|
|
m_variableDescription->setAlignment(Qt::AlignLeft|Qt::AlignTop);
|
|
|
|
|
m_variableDescription->setWordWrap(true);
|
|
|
|
|
m_variableDescription->setAttribute(Qt::WA_MacSmallSize);
|
|
|
|
|
|
|
|
|
|
QVBoxLayout *verticalLayout = new QVBoxLayout(q);
|
|
|
|
|
verticalLayout->setContentsMargins(3, 3, 3, 12);
|
|
|
|
|
verticalLayout->addWidget(m_variableTree);
|
|
|
|
|
verticalLayout->addWidget(m_variableDescription);
|
|
|
|
|
|
|
|
|
|
connect(m_variableTree, &QTreeView::clicked,
|
|
|
|
|
this, &VariableChooserPrivate::updateDescription);
|
|
|
|
|
connect(m_variableTree, &QTreeView::activated,
|
|
|
|
|
this, &VariableChooserPrivate::handleItemActivated);
|
|
|
|
|
connect(qobject_cast<QApplication *>(qApp), &QApplication::focusChanged,
|
|
|
|
|
this, &VariableChooserPrivate::updateCurrentEditor);
|
|
|
|
|
updateCurrentEditor(0, qApp->focusWidget());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 23:13:13 +02:00
|
|
|
void VariableGroupItem::populateGroup(MacroExpander *expander)
|
|
|
|
|
{
|
|
|
|
|
foreach (const QByteArray &variable, expander->variables()) {
|
|
|
|
|
auto item = new VariableItem;
|
|
|
|
|
item->m_variable = QString::fromUtf8(variable);
|
|
|
|
|
item->m_expander = expander;
|
|
|
|
|
if (variable == m_chooser->m_currentVariableName)
|
|
|
|
|
item->setFlags(Qt::ItemIsSelectable); // not ItemIsEnabled
|
|
|
|
|
appendChild(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (MacroExpander *subExpander, expander->subExpanders()) {
|
|
|
|
|
if (expander->isAccumulating()) {
|
|
|
|
|
populateGroup(subExpander);
|
|
|
|
|
} else {
|
|
|
|
|
auto item = new VariableGroupItem;
|
|
|
|
|
item->m_chooser = m_chooser;
|
|
|
|
|
item->m_expander = subExpander;
|
|
|
|
|
appendChild(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 00:34:41 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
2011-02-10 20:58:17 +01:00
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \class Core::VariableChooser
|
|
|
|
|
* \brief The VariableChooser class is used to add a tool window for selecting \QC variables
|
|
|
|
|
* to line edits, text edits or plain text edits.
|
|
|
|
|
*
|
|
|
|
|
* If you allow users to add \QC variables to strings that are specified in your UI, for example
|
|
|
|
|
* when users can provide a string through a text control, you should add a variable chooser to it.
|
|
|
|
|
* The variable chooser allows users to open a tool window that contains the list of
|
|
|
|
|
* all available variables together with a description. Double-clicking a variable inserts the
|
|
|
|
|
* corresponding string into the corresponding text control like a line edit.
|
|
|
|
|
*
|
|
|
|
|
* \image variablechooser.png "External Tools Preferences with Variable Chooser"
|
|
|
|
|
*
|
|
|
|
|
* The variable chooser monitors focus changes of all children of its parent widget.
|
|
|
|
|
* When a text control gets focus, the variable chooser checks if it has variable support set,
|
2014-10-13 18:49:44 +02:00
|
|
|
* either through the addVariableSupport() function. If the control supports variables,
|
2013-03-28 10:27:57 +01:00
|
|
|
* a tool button which opens the variable chooser is shown in it while it has focus.
|
|
|
|
|
*
|
|
|
|
|
* Supported text controls are QLineEdit, QTextEdit and QPlainTextEdit.
|
|
|
|
|
*
|
|
|
|
|
* The variable chooser is deleted when its parent widget is deleted.
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* \code
|
|
|
|
|
* QWidget *myOptionsContainerWidget = new QWidget;
|
|
|
|
|
* new Core::VariableChooser(myOptionsContainerWidget)
|
|
|
|
|
* QLineEdit *myLineEditOption = new QLineEdit(myOptionsContainerWidget);
|
|
|
|
|
* myOptionsContainerWidget->layout()->addWidget(myLineEditOption);
|
|
|
|
|
* Core::VariableChooser::addVariableSupport(myLineEditOption);
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2014-10-13 18:49:44 +02:00
|
|
|
* \internal
|
2013-03-28 10:27:57 +01:00
|
|
|
* \variable VariableChooser::kVariableSupportProperty
|
|
|
|
|
* Property name that is checked for deciding if a widget supports \QC variables.
|
|
|
|
|
* Can be manually set with
|
|
|
|
|
* \c{textcontrol->setProperty(VariableChooser::kVariableSupportProperty, true)}
|
|
|
|
|
* \sa addVariableSupport()
|
|
|
|
|
*/
|
2014-10-13 18:49:44 +02:00
|
|
|
const char kVariableSupportProperty[] = "QtCreator.VariableSupport";
|
|
|
|
|
const char kVariableNameProperty[] = "QtCreator.VariableName";
|
2013-03-28 11:05:35 +01:00
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* Creates a variable chooser that tracks all children of \a parent for variable support.
|
|
|
|
|
* Ownership is also transferred to \a parent.
|
|
|
|
|
* \sa addVariableSupport()
|
|
|
|
|
*/
|
2011-02-10 20:58:17 +01:00
|
|
|
VariableChooser::VariableChooser(QWidget *parent) :
|
|
|
|
|
QWidget(parent),
|
2014-06-20 00:34:41 +02:00
|
|
|
d(new VariableChooserPrivate(this))
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
2014-06-20 00:34:41 +02:00
|
|
|
setWindowTitle(tr("Variables"));
|
2011-02-10 20:58:17 +01:00
|
|
|
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
|
2011-06-28 15:28:29 +02:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2014-10-13 18:49:44 +02:00
|
|
|
setFocusProxy(d->m_variableTree);
|
|
|
|
|
addMacroExpanderProvider([]() { return globalMacroExpander(); });
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-02-10 20:58:17 +01:00
|
|
|
VariableChooser::~VariableChooser()
|
|
|
|
|
{
|
2014-06-20 00:34:41 +02:00
|
|
|
delete d->m_iconButton;
|
|
|
|
|
delete d;
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 18:49:44 +02:00
|
|
|
void VariableChooser::addMacroExpanderProvider(const MacroExpanderProvider &provider)
|
|
|
|
|
{
|
2014-10-20 23:13:13 +02:00
|
|
|
auto item = new VariableGroupItem;
|
|
|
|
|
item->m_chooser = d;
|
2014-10-13 18:49:44 +02:00
|
|
|
item->m_provider = provider;
|
|
|
|
|
d->m_model.rootItem()->prependChild(item);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* Marks the control as supporting variables.
|
|
|
|
|
* \sa kVariableSupportProperty
|
|
|
|
|
*/
|
2014-10-13 18:49:44 +02:00
|
|
|
void VariableChooser::addSupportedWidget(QWidget *textcontrol, const QByteArray &ownName)
|
2013-03-28 11:05:35 +01:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(textcontrol, return);
|
2014-10-13 18:49:44 +02:00
|
|
|
textcontrol->setProperty(kVariableSupportProperty, QVariant::fromValue<QWidget *>(this));
|
|
|
|
|
textcontrol->setProperty(kVariableNameProperty, ownName);
|
2013-03-28 11:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-13 18:49:44 +02:00
|
|
|
void VariableChooserPrivate::updateDescription(const QModelIndex &index)
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
2014-10-13 18:49:44 +02:00
|
|
|
m_variableDescription->setText(m_model.data(index, Qt::ToolTipRole).toString());
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-06-20 00:34:41 +02:00
|
|
|
void VariableChooserPrivate::updateCurrentEditor(QWidget *old, QWidget *widget)
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
2012-12-20 15:34:42 +01:00
|
|
|
if (old)
|
|
|
|
|
old->removeEventFilter(this);
|
2011-03-03 20:50:25 +01:00
|
|
|
if (!widget) // we might loose focus, but then keep the previous state
|
|
|
|
|
return;
|
|
|
|
|
// prevent children of the chooser itself, and limit to children of chooser's parent
|
|
|
|
|
bool handle = false;
|
|
|
|
|
QWidget *parent = widget;
|
|
|
|
|
while (parent) {
|
2014-06-20 00:34:41 +02:00
|
|
|
if (parent == q)
|
2011-03-03 20:50:25 +01:00
|
|
|
return;
|
2014-06-20 00:34:41 +02:00
|
|
|
if (parent == q->parentWidget()) {
|
2011-03-03 20:50:25 +01:00
|
|
|
handle = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
parent = parent->parentWidget();
|
|
|
|
|
}
|
|
|
|
|
if (!handle)
|
|
|
|
|
return;
|
2014-10-13 18:49:44 +02:00
|
|
|
|
2012-12-20 15:34:42 +01:00
|
|
|
widget->installEventFilter(this); // for intercepting escape key presses
|
2011-03-03 20:50:25 +01:00
|
|
|
QLineEdit *previousLineEdit = m_lineEdit;
|
2014-06-05 08:27:17 +02:00
|
|
|
QWidget *previousWidget = currentWidget();
|
2011-03-03 20:50:25 +01:00
|
|
|
m_lineEdit = 0;
|
|
|
|
|
m_textEdit = 0;
|
|
|
|
|
m_plainTextEdit = 0;
|
2014-10-13 18:49:44 +02:00
|
|
|
QWidget *chooser = widget->property(kVariableSupportProperty).value<QWidget *>();
|
|
|
|
|
m_currentVariableName = widget->property(kVariableNameProperty).value<QByteArray>();
|
|
|
|
|
bool supportsVariables = chooser == q;
|
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 (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget))
|
2011-03-03 20:50:25 +01:00
|
|
|
m_lineEdit = (supportsVariables ? lineEdit : 0);
|
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
|
|
|
else if (QTextEdit *textEdit = qobject_cast<QTextEdit *>(widget))
|
2011-03-03 20:50:25 +01:00
|
|
|
m_textEdit = (supportsVariables ? textEdit : 0);
|
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
|
|
|
else if (QPlainTextEdit *plainTextEdit = qobject_cast<QPlainTextEdit *>(widget))
|
2011-03-03 20:50:25 +01:00
|
|
|
m_plainTextEdit = (supportsVariables ? plainTextEdit : 0);
|
|
|
|
|
if (!(m_lineEdit || m_textEdit || m_plainTextEdit))
|
2014-06-20 00:34:41 +02:00
|
|
|
q->hide();
|
2014-06-05 08:27:17 +02:00
|
|
|
|
|
|
|
|
QWidget *current = currentWidget();
|
|
|
|
|
if (current != previousWidget) {
|
2011-03-03 20:50:25 +01:00
|
|
|
if (previousLineEdit)
|
|
|
|
|
previousLineEdit->setTextMargins(0, 0, 0, 0);
|
|
|
|
|
if (m_iconButton) {
|
|
|
|
|
m_iconButton->hide();
|
|
|
|
|
m_iconButton->setParent(0);
|
|
|
|
|
}
|
2014-06-05 08:27:17 +02:00
|
|
|
if (current) {
|
2011-03-03 20:50:25 +01:00
|
|
|
if (!m_iconButton)
|
|
|
|
|
createIconButton();
|
|
|
|
|
int margin = m_iconButton->pixmap().width() + 8;
|
2014-06-20 00:34:41 +02:00
|
|
|
if (q->style()->inherits("OxygenStyle"))
|
2011-03-03 20:50:25 +01:00
|
|
|
margin = qMax(24, margin);
|
2014-06-05 08:27:17 +02:00
|
|
|
if (m_lineEdit)
|
|
|
|
|
m_lineEdit->setTextMargins(0, 0, margin, 0);
|
|
|
|
|
m_iconButton->setParent(current);
|
|
|
|
|
m_iconButton->setGeometry(current->rect().adjusted(
|
|
|
|
|
current->width() - (margin + 4), 0,
|
|
|
|
|
0, -qMax(0, current->height() - (margin + 4))));
|
2011-03-03 20:50:25 +01:00
|
|
|
m_iconButton->show();
|
|
|
|
|
}
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-30 14:28:34 +02:00
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-13 18:49:44 +02:00
|
|
|
void VariableChooserPrivate::updatePositionAndShow(bool)
|
2011-03-30 14:28:34 +02:00
|
|
|
{
|
2014-06-20 00:34:41 +02:00
|
|
|
if (QWidget *w = q->parentWidget()) {
|
|
|
|
|
QPoint parentCenter = w->mapToGlobal(w->geometry().center());
|
|
|
|
|
q->move(parentCenter.x() - q->width()/2, parentCenter.y() - q->height()/2);
|
2011-03-30 14:28:34 +02:00
|
|
|
}
|
2014-06-20 00:34:41 +02:00
|
|
|
q->show();
|
|
|
|
|
q->raise();
|
|
|
|
|
q->activateWindow();
|
2014-10-20 23:13:13 +02:00
|
|
|
m_variableTree->expandAll();
|
2011-03-03 20:50:25 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-05 08:27:17 +02:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-06-20 00:34:41 +02:00
|
|
|
QWidget *VariableChooserPrivate::currentWidget()
|
2014-06-05 08:27:17 +02:00
|
|
|
{
|
|
|
|
|
if (m_lineEdit)
|
|
|
|
|
return m_lineEdit;
|
|
|
|
|
if (m_textEdit)
|
|
|
|
|
return m_textEdit;
|
|
|
|
|
return m_plainTextEdit;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-13 18:49:44 +02:00
|
|
|
void VariableChooserPrivate::handleItemActivated(const QModelIndex &index)
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
2014-10-20 23:13:13 +02:00
|
|
|
QString text = m_model.data(index, UnexpandedTextRole).toString();
|
|
|
|
|
if (!text.isEmpty())
|
|
|
|
|
insertText(text);
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-20 23:13:13 +02:00
|
|
|
void VariableChooserPrivate::insertText(const QString &text)
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
|
|
|
|
if (m_lineEdit) {
|
|
|
|
|
m_lineEdit->insert(text);
|
2011-03-03 20:50:25 +01:00
|
|
|
m_lineEdit->activateWindow();
|
2011-02-10 20:58:17 +01:00
|
|
|
} else if (m_textEdit) {
|
|
|
|
|
m_textEdit->insertPlainText(text);
|
2011-03-03 20:50:25 +01:00
|
|
|
m_textEdit->activateWindow();
|
2011-02-10 20:58:17 +01:00
|
|
|
} else if (m_plainTextEdit) {
|
|
|
|
|
m_plainTextEdit->insertPlainText(text);
|
2011-03-03 20:50:25 +01:00
|
|
|
m_plainTextEdit->activateWindow();
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-22 16:21:04 +02:00
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2012-12-20 15:34:42 +01:00
|
|
|
static bool handleEscapePressed(QKeyEvent *ke, QWidget *widget)
|
2011-06-22 16:21:04 +02:00
|
|
|
{
|
|
|
|
|
if (ke->key() == Qt::Key_Escape && !ke->modifiers()) {
|
|
|
|
|
ke->accept();
|
2012-12-20 15:34:42 +01:00
|
|
|
QTimer::singleShot(0, widget, SLOT(close()));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2014-10-20 23:13:13 +02:00
|
|
|
void VariableChooser::keyPressEvent(QKeyEvent *ev)
|
2012-12-20 15:34:42 +01:00
|
|
|
{
|
2014-10-20 23:13:13 +02:00
|
|
|
handleEscapePressed(ev, this);
|
2012-12-20 15:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2012-12-20 15:34:42 +01:00
|
|
|
bool VariableChooser::eventFilter(QObject *, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::KeyPress && isVisible()) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
|
|
|
|
|
return handleEscapePressed(ke, this);
|
2011-06-22 16:21:04 +02:00
|
|
|
}
|
2012-12-20 15:34:42 +01:00
|
|
|
return false;
|
2011-06-22 16:21:04 +02:00
|
|
|
}
|
2014-06-20 00:34:41 +02:00
|
|
|
|
|
|
|
|
} // namespace Internal
|