2011-02-10 20:58:17 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2011-02-10 20:58:17 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2011-02-10 20:58:17 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-10 20:58:17 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2011-02-10 20:58:17 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
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
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
VariableManager *vm = VariableManager::instance();
|
2011-12-21 11:29:35 +01:00
|
|
|
foreach (const QByteArray &variable, vm->variables())
|
2011-02-10 20:58:17 +01:00
|
|
|
ui->variableList->addItem(variable);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariableChooser::~VariableChooser()
|
|
|
|
|
{
|
2011-03-03 20:50:25 +01:00
|
|
|
delete m_iconButton;
|
2011-02-10 20:58:17 +01:00
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VariableChooser::updateDescription(const QString &variable)
|
|
|
|
|
{
|
|
|
|
|
if (variable.isNull())
|
|
|
|
|
ui->variableDescription->setText(m_defaultDescription);
|
|
|
|
|
else
|
2011-12-21 11:29:35 +01:00
|
|
|
ui->variableDescription->setText(VariableManager::instance()->variableDescription(variable.toUtf8()));
|
2011-02-10 20:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-03 20:50:25 +01:00
|
|
|
void VariableChooser::updateCurrentEditor(QWidget *old, QWidget *widget)
|
2011-02-10 20:58:17 +01:00
|
|
|
{
|
2011-03-03 20:50:25 +01:00
|
|
|
Q_UNUSED(old)
|
|
|
|
|
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;
|
|
|
|
|
QLineEdit *previousLineEdit = m_lineEdit;
|
|
|
|
|
m_lineEdit = 0;
|
|
|
|
|
m_textEdit = 0;
|
|
|
|
|
m_plainTextEdit = 0;
|
2011-03-07 10:23:00 +01:00
|
|
|
QVariant variablesSupportProperty = widget->property(Constants::VARIABLE_SUPPORT_PROPERTY);
|
2011-03-03 20:50:25 +01:00
|
|
|
bool supportsVariables = (variablesSupportProperty.isValid()
|
|
|
|
|
? variablesSupportProperty.toBool() : false);
|
2011-02-10 20:58:17 +01:00
|
|
|
if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget)) {
|
2011-03-03 20:50:25 +01:00
|
|
|
m_lineEdit = (supportsVariables ? lineEdit : 0);
|
2011-02-10 20:58:17 +01:00
|
|
|
} else if (QTextEdit *textEdit = qobject_cast<QTextEdit *>(widget)) {
|
2011-03-03 20:50:25 +01:00
|
|
|
m_textEdit = (supportsVariables ? textEdit : 0);
|
2011-02-10 20:58:17 +01: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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2011-02-10 20:58:17 +01:00
|
|
|
void VariableChooser::handleItemActivated(QListWidgetItem *item)
|
|
|
|
|
{
|
|
|
|
|
if (item)
|
|
|
|
|
insertVariable(item->text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
void VariableChooser::keyPressEvent(QKeyEvent *ke)
|
|
|
|
|
{
|
|
|
|
|
if (ke->key() == Qt::Key_Escape && !ke->modifiers()) {
|
|
|
|
|
ke->accept();
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(close()));
|
|
|
|
|
}
|
|
|
|
|
}
|