File: pEnvironmentVariablesEditor.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (228 lines) | stat: -rw-r--r-- 7,252 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    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 2 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 St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pEnvironmentVariablesEditor.h"
#include "pEnvironmentVariableEditor.h"

#include <QProcess>
#include <QMessageBox>
#include <QDebug>

pEnvironmentVariablesEditor::pEnvironmentVariablesEditor( QWidget* parent )
    : QWidget( parent )
{
    setupUi( this );
    verticalLayout->setMenuBar( tbActions );

    mModel = new pEnvironmentVariablesModel( this );
    tvVariables->setModel( mModel );
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    tvVariables->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
#else
    tvVariables->header()->setResizeMode( 0, QHeaderView::ResizeToContents );
#endif

    model_view_changed();

    connect( mModel, SIGNAL( defaultVariablesChanged() ), this, SLOT( model_view_changed() ) );
    connect( mModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), this, SLOT( model_view_changed() ) );
    connect( mModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), this, SLOT( model_view_changed() ) );
    connect( mModel, SIGNAL( layoutChanged() ), this, SLOT( model_view_changed() ) );
    connect( tvVariables->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ), this, SLOT( model_view_changed() ) );
}

pEnvironmentVariablesModel::Variables pEnvironmentVariablesEditor::variables() const
{
    return mModel->variables();
}

pEnvironmentVariablesModel::Variables pEnvironmentVariablesEditor::defaultVariables() const
{
    return mModel->defaultVariables();
}

QStringList pEnvironmentVariablesEditor::variables( bool keepDisabled ) const
{
    return mModel->variables( keepDisabled );
}

pEnvironmentVariablesModel::Variable pEnvironmentVariablesEditor::variable( const QString& name ) const
{
    return mModel->variable( name );
}

bool pEnvironmentVariablesEditor::contains( const QString& name ) const
{
    return mModel->contains( name );
}

bool pEnvironmentVariablesEditor::isEmpty() const
{
    return mModel->isEmpty();
}

void pEnvironmentVariablesEditor::setVariables( const pEnvironmentVariablesModel::Variables& variables, bool setDefault )
{
    mModel->setVariables( variables, setDefault );
}

void pEnvironmentVariablesEditor::setDefaultVariables( const pEnvironmentVariablesModel::Variables& variables )
{
    mModel->setDefaultVariables( variables );
}

void pEnvironmentVariablesEditor::setVariables( const QStringList& variables, bool setDefault )
{
    mModel->setVariables( variables, setDefault );
}

void pEnvironmentVariablesEditor::setVariable( const QString& name, const pEnvironmentVariablesModel::Variable& variable )
{
    mModel->setVariable( name, variable );
}

void pEnvironmentVariablesEditor::removeVariable( const QString& name )
{
    mModel->removeVariable( name );
}

void pEnvironmentVariablesEditor::clearVariables()
{
    mModel->clearVariables();
}

void pEnvironmentVariablesEditor::resetVariablesToDefault()
{
    mModel->resetVariablesToDefault();
}

void pEnvironmentVariablesEditor::resetVariablesToSystem( bool setDefault )
{
    mModel->resetVariablesToSystem( setDefault );
}

void pEnvironmentVariablesEditor::model_view_changed()
{
    const bool hasSelection = tvVariables->selectionModel()->hasSelection();

    aEdit->setEnabled( hasSelection );
    aRemove->setEnabled( hasSelection );
    aClear->setEnabled( mModel->hasChildren() );
    aResetDefault->setEnabled( !mModel->defaultVariables().isEmpty() );
}

void pEnvironmentVariablesEditor::on_aAdd_triggered()
{
    pEnvironmentVariableEditor dlg( this );
    dlg.setWindowTitle( tr( "Add a new variable..." ) );

    if ( dlg.exec() == QDialog::Rejected )
    {
        return;
    }

    if ( dlg.name().isEmpty() )
    {
        return;
    }

    pEnvironmentVariablesModel::Variable variable;

    if ( mModel->contains( dlg.name() ) )
    {
        const QMessageBox::StandardButton result = QMessageBox::question( this, QString::null, tr( "The variable '%1' already exists, update it?" ).arg( dlg.name() ), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );

        if ( result == QMessageBox::Yes )
        {
            variable = mModel->variable( dlg.name() );
        }
        else
        {
            return;
        }
    }

    variable.name = dlg.name();
    variable.value = dlg.value();
    mModel->setVariable( dlg.name(), variable );

    const QModelIndex index = mModel->index( dlg.name() );
    tvVariables->setCurrentIndex( index );
}

void pEnvironmentVariablesEditor::on_aEdit_triggered()
{
    const QModelIndex index = tvVariables->selectionModel()->selectedIndexes().value( 0 );
    pEnvironmentVariablesModel::Variable variable = mModel->variable( index );

    pEnvironmentVariableEditor dlg( this, variable.name, variable.value );
    dlg.setWindowTitle( tr( "Edit a variable..." ) );

    if ( dlg.exec() == QDialog::Rejected )
    {
        return;
    }

    variable.value = dlg.value();
    mModel->setVariable( dlg.name(), variable );
}

void pEnvironmentVariablesEditor::on_aRemove_triggered()
{
    const QModelIndex index = tvVariables->selectionModel()->selectedIndexes().value( 0 );
    const pEnvironmentVariablesModel::Variable variable = mModel->variable( index );

    const QMessageBox::StandardButton result = QMessageBox::question( this, QString::null, tr( "Are you sure you want to remove the variable '%1' ?" ).arg( variable.name ), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );

    if ( result == QMessageBox::No )
    {
        return;
    }

    mModel->removeVariable( variable.name );
}

void pEnvironmentVariablesEditor::on_aClear_triggered()
{
    const QMessageBox::StandardButton result = QMessageBox::question( this, QString::null, tr( "Are you sure you want to clear all variables?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );

    if ( result == QMessageBox::No )
    {
        return;
    }

    mModel->clearVariables();
}

void pEnvironmentVariablesEditor::on_aResetDefault_triggered()
{
    mModel->resetVariablesToDefault();
}

void pEnvironmentVariablesEditor::on_aResetSystem_triggered()
{
    mModel->resetVariablesToSystem( false );
}

void pEnvironmentVariablesEditor::on_tvVariables_activated( const QModelIndex& index )
{
    if ( index.column() == 1 )
    {
        on_aEdit_triggered();
    }
}