summaryrefslogtreecommitdiffstats
path: root/src/gui/profileDialog.cpp
blob: 2cbc155c8f675d7311ddad7f947144a9230e2f5e (plain) (blame)
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
/*
 # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # -----------------------------------------------------------------------------
 # profileDialog.cpp
 #    - GUI to define the profile.
 # -----------------------------------------------------------------------------
 */

#include "profileDialog.h"
#include "ui_profileDialog.h"
#include <src/gui/mainWindow.h>
#include <iostream>

profileDialog::profileDialog(QWidget * parent):
        QDialog(parent),
        uidiag(new Ui::Dialog)
{
    uidiag->setupUi(this);
    content = new QTableView(uidiag->widget);
    uidiag->gridLayout->addWidget(content);

    /*Das Modelfestlegen, wo die Clientname angezeigt werden.*/
    model = new QStandardItemModel(0,1,content); //Leere Tabelle mit einer Spalte erstellen
    model->setHeaderData(0, Qt::Horizontal, tr("Profile")); //Spalte name definieren
    content->setModel(model);

    QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
    content->setSelectionModel(selectionModel);
    QHeaderView *headerView = content->horizontalHeader();
    headerView->setStretchLastSection(true);

    content->setEditTriggers(QAbstractItemView::NoEditTriggers); //Die Einträge in der Tabelle werden nicht editierbar.
    content->setSelectionMode(QAbstractItemView::ExtendedSelection); //Damit mehere Einträge selektierbar werden.

    content->resizeColumnToContents(0);



    uidiag->add->setDisabled(true);
    uidiag->edit->setDisabled(true);
    uidiag->edit->setVisible(false);
    uidiag->remove->setDisabled(true);
    uidiag->load->setDisabled(true);
    uidiag->lineEdit->setDisabled(true);

    setUpProfile();

    connect(uidiag->add, SIGNAL(clicked ()), this, SLOT(AddProfile()));
    connect(uidiag->new_2, SIGNAL(clicked ()), this, SLOT(onNew()));
    connect(uidiag->load, SIGNAL(clicked ()), this, SLOT(onLoad()));
    connect(uidiag->remove, SIGNAL(clicked ()), this, SLOT(removeProfile()));
    connect(uidiag->close, SIGNAL(clicked ()), this, SLOT(accept()));
    connect(content, SIGNAL(clicked(const QModelIndex)), this, SLOT(selectionChange(const QModelIndex)));
    //show();

}

profileDialog::~profileDialog()
{
    // TODO Auto-generated destructor stub
}

void profileDialog::setUpProfile()
{
	QStringList list = MainWindow::getWindow()->getProfilList();
	for (int i=0; i<list.size(); i++)
	{
		model->insertRow(0, QModelIndex());
		model->setData(model->index(0, 0, QModelIndex()),list.at(i));
	}
}

void profileDialog::AddProfile()
{
    QString name = QString(uidiag->lineEdit->text());
    if (!ProfilList.contains(name))
    {
        if (!name.isEmpty())
        {
            model->insertRow(0, QModelIndex());
            model->setData(model->index(0, 0, QModelIndex()),name);
            ProfilList.append(name);
            MainWindow::getWindow()->saveSettings(name);
            uidiag->lineEdit->setText("");
        }
        else
        {
            QString message = QString(tr("This field can't be empty."));
            QMessageBox::information(this, "PVS", message);
        }
    }
    else
    {
        QString message = QString(tr("This name is already in the list."));
        QMessageBox::information(this, "PVS", message);
    }
}

void profileDialog::removeProfile()
{
    QTableView *temp = static_cast<QTableView*>(content);
    QItemSelectionModel *selectionModel = temp->selectionModel();

    QModelIndexList indexes = selectionModel->selectedRows();
    QModelIndex index;

    foreach(index, indexes)
    {
        int row = content->currentIndex().row();
        QString current = model->index(content->currentIndex().row(), 0).data(Qt::DisplayRole).toString();
        model->removeRow(row, QModelIndex());
        ProfilList.removeOne(current);
        MainWindow::getWindow()->removeProfil(current);
    }


   /* if (temp->model()->rowCount()<=0)
    {*/
        uidiag->remove->setDisabled(true);
        uidiag->edit->setDisabled(true);
        uidiag->add->setDisabled(true);
        uidiag->load->setDisabled(true);
    //}
}

void profileDialog::onNew()
{
    uidiag->add->setDisabled(false);
    uidiag->lineEdit->setDisabled(false);
    uidiag->lineEdit->setFocus();
}

void profileDialog::onLoad()
{
	QTableView *temp = static_cast<QTableView*>(content);
	QItemSelectionModel *selectionModel = temp->selectionModel();

	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;
	if (indexes.size()>1)
	{
		QMessageBox::information(this, "PVS", tr("You can only load one profile at time"));
	}
	else if(indexes.size() == 1)
	{
		foreach(index, indexes)
		    {
		        //int row = content->currentIndex().row();
		        QString current = model->index(content->currentIndex().row(), 0).data(Qt::DisplayRole).toString();
		        MainWindow::getWindow()->loadSettings(current);
		    }
	}

	emit accept();
}

void profileDialog::selectionChange(const QModelIndex & index)
{
    uidiag->remove->setDisabled(false);
    uidiag->edit->setDisabled(false);
    uidiag->add->setDisabled(false);
    uidiag->load->setDisabled(false);

}

void profileDialog::close()
{
    close();
}