Commit e5c51f44 authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesigner: Add AddTabToTableView dialog



Change-Id: I628bc7061242bccf00701c77704f4b1e553019ff
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 5e638c0b
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
** GNU Lesser General Public License Usage
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "addtabtotabviewdialog.h"
#include "ui_addtabtotabviewdialog.h"
#include <QtDebug>
AddTabToTabViewDialog::AddTabToTabViewDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddTabToTabViewDialog)
{
ui->setupUi(this);
ui->addTabLineEdit->setForceFirstCapitalLetter(true);
}
AddTabToTabViewDialog::~AddTabToTabViewDialog()
{
delete ui;
}
QString AddTabToTabViewDialog::create(const QString &tabName, QWidget *parent)
{
AddTabToTabViewDialog addTabToTabViewDialog(parent);
Utils::FileNameValidatingLineEdit *fileNameValidatingLineEdit = addTabToTabViewDialog.ui->addTabLineEdit;
fileNameValidatingLineEdit->setText(tabName);
int result = addTabToTabViewDialog.exec();
qDebug() << "validate: " << fileNameValidatingLineEdit->errorMessage();
if (result == QDialog::Accepted && fileNameValidatingLineEdit->isValid())
return fileNameValidatingLineEdit->text();
else
return QString();
}
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
** GNU Lesser General Public License Usage
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef ADDTABTOTABVIEWDIALOG_H
#define ADDTABTOTABVIEWDIALOG_H
#include <QDialog>
namespace Ui {
class AddTabToTabViewDialog;
}
class AddTabToTabViewDialog : public QDialog
{
Q_OBJECT
public:
explicit AddTabToTabViewDialog(QWidget *parent = 0);
~AddTabToTabViewDialog();
static QString create(const QString &tabName, QWidget *parent);
private:
Ui::AddTabToTabViewDialog *ui;
};
#endif // ADDTABTOTABVIEWDIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AddTabToTabViewDialog</class>
<widget class="QDialog" name="AddTabToTabViewDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>362</width>
<height>80</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="addTabLabel">
<property name="text">
<string>Add Tab</string>
</property>
</widget>
</item>
<item>
<widget class="Utils::FileNameValidatingLineEdit" name="addTabLineEdit"/>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Utils::FileNameValidatingLineEdit</class>
<extends>QLineEdit</extends>
<header location="global">utils/filenamevalidatinglineedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AddTabToTabViewDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AddTabToTabViewDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
VPATH += $$PWD
SOURCES += modelnodecontextmenu.cpp
SOURCES += addtabtotabviewdialog.cpp
SOURCES += designeractionmanagerview.cpp
SOURCES += tabviewdesigneraction.cpp
SOURCES += defaultdesigneraction.cpp
......@@ -11,6 +12,7 @@ SOURCES += modelnodeoperations.cpp
SOURCES += crumblebar.cpp
HEADERS += modelnodecontextmenu.h
HEADERS += addtabtotabviewdialog.h
HEADERS += designeractionmanagerview.h
HEADERS += tabviewdesigneraction.h
HEADERS += defaultdesigneraction.h
......@@ -21,3 +23,5 @@ HEADERS += designeractionmanager.h
HEADERS += modelnodeoperations.h
HEADERS += abstractdesigneraction.h
HEADERS += crumblebar.h
FORMS += addtabtotabviewdialog.ui
......@@ -29,11 +29,23 @@
#include "tabviewdesigneraction.h"
#include <QCoreApplication>
#include <QUrl>
#include <QFileInfo>
#include <QMessageBox>
#include <utils/textfileformat.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>
#include <coreplugin/icore.h>
#include <nodemetainfo.h>
#include <modelnode.h>
#include <nodeabstractproperty.h>
#include "addtabtotabviewdialog.h"
#include <QCoreApplication>
namespace QmlDesigner {
......@@ -50,8 +62,9 @@ bool isTabAndParentIsTabView(const ModelNode &modelNode)
}
TabViewDesignerAction::TabViewDesignerAction()
: DefaultDesignerAction(QCoreApplication::translate("TabViewToolAction","Edit Tabs"))
: DefaultDesignerAction(QCoreApplication::translate("TabViewToolAction","Add Tab..."))
{
connect(action(), SIGNAL(triggered()), this, SLOT(addNewTab()));
}
QByteArray TabViewDesignerAction::category() const
......@@ -92,4 +105,74 @@ bool TabViewDesignerAction::isEnabled(const SelectionContext &selectionContext)
return isVisible(selectionContext);
}
bool TabViewDesignerAction::createFile(const QString &filePath)
{
Utils::TextFileFormat textFileFormat;
textFileFormat.codec = Core::EditorManager::defaultTextCodec();
QString errorMessage;
const QString componentString("import QtQuick 2.1\nimport QtQuick.Controls 1.0\n\nItem {\n\n}");
return textFileFormat.writeFile(filePath, componentString, &errorMessage);
}
void TabViewDesignerAction::addNewFileToVersionControl(const QString &directoryPath, const QString &newFilePath)
{
Core::IVersionControl *versionControl = Core::VcsManager::findVersionControlForDirectory(directoryPath);
if (versionControl && versionControl->supportsOperation(Core::IVersionControl::AddOperation)) {
const QMessageBox::StandardButton button =
QMessageBox::question(Core::ICore::mainWindow(),
Core::VcsManager::msgAddToVcsTitle(),
Core::VcsManager::msgPromptToAddToVcs(QStringList(newFilePath), versionControl),
QMessageBox::Yes | QMessageBox::No);
if (button == QMessageBox::Yes && !versionControl->vcsAdd(newFilePath)) {
QMessageBox::warning(Core::ICore::mainWindow(),
Core::VcsManager::msgAddToVcsFailedTitle(),
Core::VcsManager::msgToAddToVcsFailed(QStringList(newFilePath), versionControl));
}
}
}
static ModelNode findTabViewModelNode(const ModelNode &currentModelNode)
{
if (currentModelNode.metaInfo().isSubclassOf("QtQuick.Controls.TabView", -1, -1))
return currentModelNode;
else
return findTabViewModelNode(currentModelNode.parentProperty().parentModelNode());
}
void TabViewDesignerAction::addNewTab()
{
QString tabName = AddTabToTabViewDialog::create(QLatin1String("Tab"), Core::ICore::mainWindow());
if (!tabName.isEmpty()) {
QString directoryPath = QFileInfo(selectionContext().view()->model()->fileUrl().toLocalFile()).absolutePath();
QString newFilePath = directoryPath +QLatin1String("/") + tabName + QLatin1String(".qml");
if (QFileInfo(newFilePath).exists()) {
QMessageBox::warning(Core::ICore::mainWindow(), tr("Name error"), tr("Component already exists!"));
} else {
bool fileCreated = createFile(newFilePath);
if (fileCreated) {
addNewFileToVersionControl(directoryPath, newFilePath);
ModelNode tabViewModelNode = findTabViewModelNode(selectionContext().currentSingleSelectedNode());
PropertyListType propertyList;
propertyList.append(QPair<PropertyName, QVariant>("source", QString(tabName + QLatin1String(".qml"))));
propertyList.append(QPair<PropertyName, QVariant>("title", tabName));
ModelNode newTabModelNode = selectionContext().view()->createModelNode("QtQuick.Controls.Tab",
tabViewModelNode.majorVersion(),
tabViewModelNode.minorVersion(),
propertyList);
newTabModelNode.setId(newTabModelNode.view()->generateNewId(tabName));
tabViewModelNode.defaultNodeAbstractProperty().reparentHere(newTabModelNode);
}
}
}
}
} // namespace QmlDesigner
......@@ -34,8 +34,9 @@
namespace QmlDesigner {
class TabViewDesignerAction : public DefaultDesignerAction
class TabViewDesignerAction : public QObject, public DefaultDesignerAction
{
Q_OBJECT
public:
TabViewDesignerAction();
......@@ -47,6 +48,12 @@ public:
protected:
bool isVisible(const SelectionContext &selectionContext) const;
bool isEnabled(const SelectionContext &selectionContext) const;
bool createFile(const QString &filePath);
void addNewFileToVersionControl(const QString &directoryPath, const QString &newFileName);
private slots:
void addNewTab();
};
} // namespace QmlDesigner
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment