saveitemsdialog.cpp 4.48 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
35
#include "saveitemsdialog.h"
#include "mainwindow.h"
#include "vcsmanager.h"

#include <coreplugin/ifile.h>

Alessandro Portale's avatar
Alessandro Portale committed
36
#include <QtCore/QDir>
con's avatar
con committed
37
#include <QtCore/QFileInfo>
mae's avatar
compile    
mae committed
38
#include <QtCore/QDir>
con's avatar
con committed
39
40
41
42
43
44
#include <QtGui/QPushButton>
#include <QtGui/QTreeWidget>
#include <QtGui/QHeaderView>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>

con's avatar
con committed
45
46
Q_DECLARE_METATYPE(Core::IFile*);

con's avatar
con committed
47
48
49
using namespace Core;
using namespace Core::Internal;

con's avatar
con committed
50
SaveItemsDialog::SaveItemsDialog(QWidget *parent,
51
                                 QList<IFile *> items)
con's avatar
con committed
52
    : QDialog(parent)
con's avatar
con committed
53
54
{
    m_ui.setupUi(this);
con's avatar
con committed
55
56
57
58
    QPushButton *discardButton = m_ui.buttonBox->addButton(tr("Don't Save"), QDialogButtonBox::DestructiveRole);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setDefault(true);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setFocus(Qt::TabFocusReason);
    m_ui.buttonBox->button(QDialogButtonBox::Save)->setMinimumWidth(130); // bad magic number to avoid resizing of button
con's avatar
con committed
59

60
61
    m_ui.saveBeforeBuildCheckBox->setVisible(false);

62
    foreach (IFile *file, items) {
con's avatar
con committed
63
64
        QString visibleName;
        QString directory;
65
        QString fileName = file->fileName();
con's avatar
con committed
66
        if (fileName.isEmpty()) {
67
            visibleName = file->suggestedFileName();
con's avatar
con committed
68
69
70
71
72
73
        } else {
            QFileInfo info = QFileInfo(fileName);
            directory = info.absolutePath();
            visibleName = info.fileName();
        }
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList()
74
                                                    << visibleName << QDir::toNativeSeparators(directory));
75
        item->setData(0, Qt::UserRole, qVariantFromValue(file));
con's avatar
con committed
76
77
78
    }

    m_ui.treeWidget->resizeColumnToContents(0);
con's avatar
con committed
79
80
    m_ui.treeWidget->selectAll();
    updateSaveButton();
con's avatar
con committed
81

con's avatar
con committed
82
    connect(m_ui.buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()),
con's avatar
con committed
83
84
            this, SLOT(collectItemsToSave()));
    connect(discardButton, SIGNAL(clicked()), this, SLOT(discardAll()));
con's avatar
con committed
85
    connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateSaveButton()));
con's avatar
con committed
86
87
88
89
90
91
92
}

void SaveItemsDialog::setMessage(const QString &msg)
{
    m_ui.msgLabel->setText(msg);
}

con's avatar
con committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
void SaveItemsDialog::updateSaveButton()
{
    int count = m_ui.treeWidget->selectedItems().count();
    QPushButton *button = m_ui.buttonBox->button(QDialogButtonBox::Save);
    if (count == m_ui.treeWidget->topLevelItemCount()) {
        button->setEnabled(true);
        button->setText(tr("Save All"));
    } else if (count == 0) {
        button->setEnabled(false);
        button->setText(tr("Save"));
    } else {
        button->setEnabled(true);
        button->setText(tr("Save Selected"));
    }
}

con's avatar
con committed
109
110
111
void SaveItemsDialog::collectItemsToSave()
{
    m_itemsToSave.clear();
con's avatar
con committed
112
113
    foreach (QTreeWidgetItem *item, m_ui.treeWidget->selectedItems()) {
        m_itemsToSave.append(item->data(0, Qt::UserRole).value<IFile*>());
con's avatar
con committed
114
115
116
117
118
119
    }
    accept();
}

void SaveItemsDialog::discardAll()
{
con's avatar
con committed
120
    m_ui.treeWidget->clearSelection();
con's avatar
con committed
121
122
123
124
125
126
127
    collectItemsToSave();
}

QList<IFile*> SaveItemsDialog::itemsToSave() const
{
    return m_itemsToSave;
}
128
129
130
131
132
133
134
135
136
137
138

void SaveItemsDialog::setAlwaysSaveMessage(const QString &msg)
{
    m_ui.saveBeforeBuildCheckBox->setText(msg);
    m_ui.saveBeforeBuildCheckBox->setVisible(true);
}

bool SaveItemsDialog::alwaysSaveChecked()
{
    return m_ui.saveBeforeBuildCheckBox->isChecked();
}