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
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/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
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** 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"
|
|
|
|
|
#include "ui_variablechooser.h"
|
|
|
|
|
#include "variablemanager.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
|
2013-03-28 11:05:35 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2011-08-17 12:54:58 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QTextEdit>
|
|
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QListWidgetItem>
|
2011-06-22 16:21:04 +02:00
|
|
|
|
2011-02-10 20:58:17 +01:00
|
|
|
using namespace Core;
|
|
|
|
|
|
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,
|
2013-10-07 13:34:40 +02:00
|
|
|
* either through the addVariableSupport() function or by manually setting the
|
2013-03-28 10:27:57 +01:00
|
|
|
* custom kVariableSupportProperty on the control. If the control supports variables,
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \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()
|
|
|
|
|
*/
|
2013-03-28 11:05:35 +01:00
|
|
|
const char VariableChooser::kVariableSupportProperty[] = "QtCreator.VariableSupport";
|
|
|
|
|
|
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),
|
2011-11-23 15:15:01 +00:00
|
|
|
ui(new Internal::Ui::VariableChooser),
|
2011-02-10 20:58:17 +01:00
|
|
|
m_lineEdit(0),
|
|
|
|
|
m_textEdit(0),
|
|
|
|
|
m_plainTextEdit(0)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
m_defaultDescription = ui->variableDescription->text();
|
|
|
|
|
ui->variableList->setAttribute(Qt::WA_MacSmallSize);
|
|
|
|
|
ui->variableList->setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
|
|
|
ui->variableDescription->setAttribute(Qt::WA_MacSmallSize);
|
|
|
|
|
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
|
2011-06-28 15:28:29 +02:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2011-02-10 20:58:17 +01:00
|
|
|
setFocusProxy(ui->variableList);
|
|
|
|
|
|
2013-03-11 11:52:05 +01:00
|
|
|
foreach (const QByteArray &variable, VariableManager::variables())
|
2012-11-18 22:33:50 +02:00
|
|
|
ui->variableList->addItem(QString::fromLatin1(variable));
|
2011-02-10 20:58:17 +01:00
|
|
|
|
|
|
|
|
connect(ui->variableList, SIGNAL(currentTextChanged(QString)),
|
|
|
|
|
this, SLOT(updateDescription(QString)));
|
|
|
|
|
connect(ui->variableList, SIGNAL(itemActivated(QListWidgetItem*)),
|
|
|
|
|
this, SLOT(handleItemActivated(QListWidgetItem*)));
|
|
|
|
|
connect(qApp, SIGNAL(focusChanged(QWidget*,QWidget*)),
|
2011-03-03 20:50:25 +01:00
|
|
|
this, SLOT(updateCurrentEditor(QWidget*,QWidget*)));
|
|
|
|
|
updateCurrentEditor(0, qApp->focusWidget());
|
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()
|
|
|
|
|
{
|
2011-03-03 20:50:25 +01:00
|
|
|
delete m_iconButton;
|
2011-02-10 20:58:17 +01:00
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* Marks the control as supporting variables.
|
|
|
|
|
* \sa kVariableSupportProperty
|
|
|
|
|
*/
|
2013-03-28 11:05:35 +01:00
|
|
|
void VariableChooser::addVariableSupport(QWidget *textcontrol)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(textcontrol, return);
|
|
|
|
|
textcontrol->setProperty(kVariableSupportProperty, true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-02-10 20:58:17 +01:00
|
|
|
void VariableChooser::updateDescription(const QString &variable)
|
|
|
|
|
{
|
|
|
|
|
if (variable.isNull())
|
|
|
|
|
ui->variableDescription->setText(m_defaultDescription);
|
|
|
|
|
else
|
2013-03-11 11:52:05 +01:00
|
|
|
ui->variableDescription->setText(VariableManager::variableDescription(variable.toUtf8()));
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-03-03 20:50:25 +01:00
|
|
|
void VariableChooser::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) {
|
|
|
|
|
if (parent == this)
|
|
|
|
|
return;
|
|
|
|
|
if (parent == this->parentWidget()) {
|
|
|
|
|
handle = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
parent = parent->parentWidget();
|
|
|
|
|
}
|
|
|
|
|
if (!handle)
|
|
|
|
|
return;
|
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;
|
|
|
|
|
m_lineEdit = 0;
|
|
|
|
|
m_textEdit = 0;
|
|
|
|
|
m_plainTextEdit = 0;
|
2013-03-28 11:05:35 +01:00
|
|
|
QVariant variablesSupportProperty = widget->property(kVariableSupportProperty);
|
2011-03-03 20:50:25 +01:00
|
|
|
bool supportsVariables = (variablesSupportProperty.isValid()
|
|
|
|
|
? variablesSupportProperty.toBool() : false);
|
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))
|
|
|
|
|
hide();
|
|
|
|
|
if (m_lineEdit != previousLineEdit) {
|
|
|
|
|
if (previousLineEdit)
|
|
|
|
|
previousLineEdit->setTextMargins(0, 0, 0, 0);
|
|
|
|
|
if (m_iconButton) {
|
|
|
|
|
m_iconButton->hide();
|
|
|
|
|
m_iconButton->setParent(0);
|
|
|
|
|
}
|
|
|
|
|
if (m_lineEdit) {
|
|
|
|
|
if (!m_iconButton)
|
|
|
|
|
createIconButton();
|
|
|
|
|
int margin = m_iconButton->pixmap().width() + 8;
|
|
|
|
|
if (style()->inherits("OxygenStyle"))
|
|
|
|
|
margin = qMax(24, margin);
|
|
|
|
|
m_lineEdit->setTextMargins(0, 0, margin, 0);
|
|
|
|
|
m_iconButton->setParent(m_lineEdit);
|
|
|
|
|
m_iconButton->setGeometry(m_lineEdit->rect().adjusted(
|
|
|
|
|
m_lineEdit->width() - (margin + 4), 0, 0, 0));
|
|
|
|
|
m_iconButton->show();
|
|
|
|
|
}
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-03-03 20:50:25 +01:00
|
|
|
void VariableChooser::createIconButton()
|
|
|
|
|
{
|
|
|
|
|
m_iconButton = new Utils::IconButton;
|
|
|
|
|
m_iconButton->setPixmap(QPixmap(QLatin1String(":/core/images/replace.png")));
|
|
|
|
|
m_iconButton->setToolTip(tr("Insert variable"));
|
|
|
|
|
m_iconButton->hide();
|
2011-03-30 14:28:34 +02:00
|
|
|
connect(m_iconButton, SIGNAL(clicked()), this, SLOT(updatePositionAndShow()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-03-30 14:28:34 +02:00
|
|
|
void VariableChooser::updatePositionAndShow()
|
|
|
|
|
{
|
|
|
|
|
if (parentWidget()) {
|
|
|
|
|
QPoint parentCenter = parentWidget()->mapToGlobal(parentWidget()->geometry().center());
|
|
|
|
|
move(parentCenter.x() - width()/2, parentCenter.y() - height()/2);
|
|
|
|
|
}
|
|
|
|
|
show();
|
2011-06-28 15:28:29 +02:00
|
|
|
raise();
|
|
|
|
|
activateWindow();
|
2011-03-03 20:50:25 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-02-10 20:58:17 +01:00
|
|
|
void VariableChooser::handleItemActivated(QListWidgetItem *item)
|
|
|
|
|
{
|
|
|
|
|
if (item)
|
|
|
|
|
insertVariable(item->text());
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 10:27:57 +01:00
|
|
|
/*!
|
|
|
|
|
* \internal
|
|
|
|
|
*/
|
2011-02-10 20:58:17 +01:00
|
|
|
void VariableChooser::insertVariable(const QString &variable)
|
|
|
|
|
{
|
2011-03-21 09:01:46 +01:00
|
|
|
const QString &text = QLatin1String("%{") + variable + QLatin1String("}");
|
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
|
|
|
|
|
*/
|
2012-12-20 15:34:42 +01:00
|
|
|
void VariableChooser::keyPressEvent(QKeyEvent *ke)
|
|
|
|
|
{
|
|
|
|
|
handleEscapePressed(ke, this);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|