summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/Project/EditPresentationIdDlg.cpp
blob: 09e1da370c27821f4482c74b4a2988f933d9755e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "EditPresentationIdDlg.h"
#include "ui_EditPresentationIdDlg.h"
#include "StudioApp.h"
#include "Core.h"
#include "Dialogs.h"

EditPresentationIdDlg::EditPresentationIdDlg(const QString &src, DialogType type, QWidget *parent)
    : QDialog(parent)
    , m_src(src)
    , m_ui(new Ui::EditPresentationIdDlg)
    , m_dialogType(type)
{
    m_ui->setupUi(this);

    switch (m_dialogType) {
    case EditPresentationId:
        m_ui->label->setText(tr("Presentation Id"));
        setWindowTitle(tr("Edit Presentation Id"));
        break;
    case EditQmlStreamId:
        m_ui->label->setText(tr("Qml Stream Id"));
        setWindowTitle(tr("Edit Qml Stream Id"));
        break;
    case EditPresentationName:
        m_ui->label->setText(tr("Presentation Name"));
        setWindowTitle(tr("Rename Presentation"));
        break;
    case EditQmlStreamName:
        m_ui->label->setText(tr("Qml Stream Name"));
        setWindowTitle(tr("Rename Qml Stream"));
        break;
    default:
        break;
    }

    if (m_dialogType == EditPresentationId || m_dialogType == EditQmlStreamId) {
        m_presentationId = g_StudioApp.GetCore()->getProjectFile().getPresentationId(src);
        m_ui->lineEditPresentationId->setText(m_presentationId);
    } else {
        QFileInfo fi(src);
        m_ui->lineEditPresentationId->setText(fi.fileName());
    }

    window()->setFixedSize(size());
}

void EditPresentationIdDlg::accept()
{
    QString newValue = m_ui->lineEditPresentationId->text();
    if (newValue.isEmpty()) {
        displayWarning(EmptyWarning);
    } else if (m_dialogType == EditPresentationId || m_dialogType == EditQmlStreamId) {
        if (newValue != m_presentationId) {
            if (!g_StudioApp.GetCore()->getProjectFile().isUniquePresentationId(newValue, m_src)) {
                displayWarning(UniqueWarning);
            } else {
                g_StudioApp.GetCore()->getProjectFile().writePresentationId(newValue, m_src);
                QDialog::accept();
            }
        } else {
            QDialog::accept();
        }
    } else { // editing name
        QFileInfo fi(m_src);
        QString suffix = QStringLiteral(".") + fi.suffix();
        if (!newValue.endsWith(suffix))
            newValue.append(suffix);

        if (newValue == suffix) {
            // If we are left with just the suffix, treat it as an empty name
            displayWarning(EmptyWarning);
        } else {
            int slashIndex = m_src.lastIndexOf(QLatin1Char('/'));
            if (slashIndex >= 0)
                newValue.prepend(m_src.left(slashIndex + 1));

            if (newValue != m_src) {
                if (g_StudioApp.GetCore()->getProjectFile().renamePresentationFile(m_src, newValue))
                    QDialog::accept();
                else
                    displayWarning(UniqueWarning);
            } else {
                QDialog::accept();
            }
        }
    }
}

void EditPresentationIdDlg::displayWarning(WarningType warningType)
{
    QString warning;
    switch (m_dialogType) {
    // Presentation Id warnings are also displayed from preferences dialog, so they are handled
    // by CStudioApp.
    case EditPresentationId:
        if (warningType == EmptyWarning)
            g_StudioApp.showPresentationIdEmptyWarning();
        else
            g_StudioApp.showPresentationIdUniqueWarning();
        return;
    case EditQmlStreamId:
        if (warningType == EmptyWarning)
            warning = tr("Qml stream Id must not be empty.");
        else
            warning = tr("Qml stream Id must be unique.");
        break;
    case EditPresentationName:
        if (warningType == EmptyWarning)
            warning = tr("Presentation name must not be empty.");
        else
            warning = tr("Renaming presentation failed.\n"
                         "The new name must be unique within its folder and a valid filename.");
        break;
    case EditQmlStreamName:
        if (warningType == EmptyWarning)
            warning = tr("Qml stream name must not be empty.");
        else
            warning = tr("Renaming Qml stream failed.\n"
                         "The new name must be unique within its folder and a valid filename.");
        break;
    default:
        break;
    }

    g_StudioApp.GetDialogs()->DisplayMessageBox(tr("Warning"), warning,
                                                Qt3DSMessageBox::ICON_WARNING, false);
}

EditPresentationIdDlg::~EditPresentationIdDlg()
{
    delete m_ui;
}