File: ItemDialog.cpp

package info (click to toggle)
gm-assistant 1.2.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,116 kB
  • sloc: cpp: 7,281; makefile: 3
file content (211 lines) | stat: -rw-r--r-- 6,736 bytes parent folder | download | duplicates (3)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*************************************************************************
* Copyright © 2011-2021 Vincent Prat & Simon Nicolas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*************************************************************************/

#include "ItemDialog.h"
#include <QMessageBox>

ItemDialog::ItemDialog(QWidget *parent): QDialog(parent), audioBrowser(new QFileDialog(this, QApplication::translate("itemDialog", "Select the audio file to associate to the item", 0)))
{
    setupUi(this);
    // setting the audio browser up
    audioBrowser->setOption(QFileDialog::DontUseNativeDialog, true);
    audioBrowser->setFileMode(QFileDialog::ExistingFile);
    audioBrowser->setReadOnly(true);
    audioBrowser->setOption(QFileDialog::HideNameFilterDetails, true);
}

Item::State ItemDialog::state() const
{
    if (radioNone->isChecked())
        return Item::sNone;
    else if (radioProgress->isChecked())
        return Item::sProgress;
    else if (radioFailure->isChecked())
        return Item::sFailure;
    else
        return Item::sSuccess;
}

QString ItemDialog::text() const
{
    return editItem->text();
}

Item::Type ItemDialog::type() const
{
    if (radioSound->isChecked())
        return Item::tSound;
    else if (radioImage->isChecked())
        return Item::tImage;
    else
        return Item::tBasic;
}

QString ItemDialog::fileName() const
{
    QString result(editFile->text()); // file selected
    QString directory(QDir::current().path()); // current directory
    if (result.startsWith(directory))
    {
        return result.replace(0,directory.length()+1,"");
    }

    return result;
}

void ItemDialog::on_pushChild_clicked()
{
    if (editItem->text()!="")
    {
        if (radioBasic->isChecked() || editFile->text()!="")
        {
            rRes = rChild;
            accept();
        }
        else
        {
        QMessageBox::critical(this,QApplication::translate("itemDialog","Incomplete data",0),QApplication::translate("itemDialog","You must select a file before validating.",0));
        }
    }
    else
    {
        QMessageBox::critical(this,QApplication::translate("itemDialog","Incomplete data",0),QApplication::translate("itemDialog","You must fill the content before validating.",0));
    }
}

void ItemDialog::on_pushBrother_clicked()
{
    if (editItem->text()!="")
    {
        if (radioBasic->isChecked() || editFile->text()!="")
        {
            rRes = rBrother;
            accept();
        }
        else
        {
        QMessageBox::critical(this,QApplication::translate("itemDialog","Incomplete data",0),QApplication::translate("itemDialog","You must select a file before validating.",0));
        }
    }
    else
    {
        QMessageBox::critical(this,QApplication::translate("itemDialog","Incomplete data",0),QApplication::translate("itemDialog","You must fill the content before validating.",0));
    }
}

ItemDialog::Result ItemDialog::selectionResult() const
{
    return rRes;
}

void ItemDialog::on_radioBasic_clicked()
{
    toolBrowse->setEnabled(false);
}

void ItemDialog::on_radioSound_clicked()
{
    toolBrowse->setEnabled(true);
    editFile->setText("");
}

void ItemDialog::on_radioImage_clicked()
{
    toolBrowse->setEnabled(true);
    editFile->setText("");
}

void ItemDialog::on_toolBrowse_clicked()
{
    switch (type())
    {
        case Item::tSound:  {
                                if (audioBrowser->exec())
                                {
                                    editFile->setText(audioBrowser->selectedFiles()[0]);
                                }    
                                break;
                            }
        case Item::tImage:  { 
                                editFile->setText(QFileDialog::getOpenFileName(this,QApplication::translate("itemDialog","Select the image file to associate to the item",0),"",QApplication::translate("itemDialog","Image files (*.jpg *.jpeg *.png *.bmp *.svg)",0),0,QFileDialog::DontUseNativeDialog));
                                break;
                            }
        default:            break;
    }
}

int ItemDialog::exec(Item *item)
{
    // initialization
    QString content, file;
    Item::Type itemType = Item::tBasic;
    Item::State itemState = Item::sNone;
    if (item)
    {
        setWindowTitle(QApplication::translate("itemDialog", "Edit the item", 0));
        pushBrother->setVisible(false);
        pushChild->setIcon(QIcon(":/data/images/check.svg"));
        pushChild->setText(QApplication::translate("itemDialog", "&Validate", 0));
        pushChild->setDefault(true);
        // pre-filling
        content = item->content().c_str();
        itemType = item->type();
        itemState = item->state();
        if (itemType != Item::tBasic)
        {
            file = dynamic_cast<FileItem*>(item)->fileName().c_str();
        }
    }
    else
    {
        setWindowTitle(QApplication::translate("itemDialog", "Create a new item", 0));
        pushBrother->setVisible(true);
        pushChild->setIcon(QIcon(":/data/images/son.svg"));
        pushChild->setText(QApplication::translate("itemDialog", "C&hild", 0));
        pushBrother->setDefault(true);
    }

    // resets the window
    // state
    radioNone->setChecked(itemState == Item::sNone);
    radioProgress->setChecked(itemState == Item::sProgress);
    radioSuccess->setChecked(itemState == Item::sSuccess);
    radioFailure->setChecked(itemState == Item::sFailure);
    // type
    radioBasic->setChecked(itemType == Item::tBasic);
    radioSound->setChecked(itemType == Item::tSound);
    radioImage->setChecked(itemType == Item::tImage);
    // everuthing else
    toolBrowse->setEnabled(itemType != Item::tBasic);

    editItem->setText(content);
    editItem->setFocus();
    editFile->setText(file);

    // shows it
    return QDialog::exec();
}

void ItemDialog::changeEvent(QEvent *e)
{
    if (e->type() == QEvent::LanguageChange)
    {
        retranslateUi(this);
        audioBrowser->setNameFilter(QApplication::translate("itemDialog", "Audio files (*)", 0));
    }
}