aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/dialogs/saveitemsdialog.cpp
blob: 8ad7b8e7492dd2bec1127a7e0b2165fa324b9ae8 (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
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "saveitemsdialog.h"

#include <coreplugin/diffservice.h>
#include <coreplugin/idocument.h>

#include <utils/filepath.h>
#include <utils/fsengine/fileiconprovider.h>
#include <utils/hostosinfo.h>
#include <utils/layoutbuilder.h>

#include <extensionsystem/pluginmanager.h>

#include <QCheckBox>
#include <QDebug>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileInfo>
#include <QLabel>
#include <QPushButton>
#include <QTreeWidget>

Q_DECLARE_METATYPE(Core::IDocument*)

using namespace Core;
using namespace Core::Internal;

SaveItemsDialog::SaveItemsDialog(QWidget *parent, const QList<IDocument *> &items)
    : QDialog(parent)
    , m_msgLabel(new QLabel(tr("The following files have unsaved changes:")))
    , m_treeWidget(new QTreeWidget)
    , m_saveBeforeBuildCheckBox(new QCheckBox(tr("Automatically save all files before building")))
    , m_buttonBox(new QDialogButtonBox)
{
    resize(457, 200);
    setWindowTitle(tr("Save Changes"));

    m_treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_treeWidget->setTextElideMode(Qt::ElideLeft);
    m_treeWidget->setIndentation(0);
    m_treeWidget->setRootIsDecorated(false);
    m_treeWidget->setUniformRowHeights(true);
    m_treeWidget->setHeaderHidden(true);
    m_treeWidget->setColumnCount(2);

    // QDialogButtonBox's behavior for "destructive" is wrong, the "do not save" should be left-aligned
    const QDialogButtonBox::ButtonRole discardButtonRole = Utils::HostOsInfo::isMacHost()
                                                               ? QDialogButtonBox::ResetRole
                                                               : QDialogButtonBox::DestructiveRole;
    if (DiffService::instance()) {
        m_diffButton = m_buttonBox->addButton(tr("&Diff"), discardButtonRole);
        connect(m_diffButton, &QAbstractButton::clicked, this, &SaveItemsDialog::collectFilesToDiff);
    }
    m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Save);
    QPushButton *discardButton = m_buttonBox->addButton(tr("Do &Not Save"), discardButtonRole);
    m_buttonBox->button(QDialogButtonBox::Save)->setDefault(true);
    m_treeWidget->setFocus();

    m_saveBeforeBuildCheckBox->setVisible(false);

    using namespace Utils::Layouting;
    // clang-format off
    Column {
        m_msgLabel,
        m_treeWidget,
        m_saveBeforeBuildCheckBox,
        m_buttonBox
    }.attachTo(this);
    // clang-format on

    for (IDocument *document : items) {
        QString visibleName;
        QString directory;
        Utils::FilePath filePath = document->filePath();
        if (filePath.isEmpty()) {
            visibleName = document->fallbackSaveAsFileName();
        } else {
            directory = filePath.absolutePath().toUserOutput();
            visibleName = filePath.fileName();
        }
        QTreeWidgetItem *item = new QTreeWidgetItem(m_treeWidget,
                                                    QStringList()
                                                        << visibleName
                                                        << QDir::toNativeSeparators(directory));
        if (!filePath.isEmpty())
            item->setIcon(0, Utils::FileIconProvider::icon(filePath));
        item->setData(0, Qt::UserRole, QVariant::fromValue(document));
    }

    m_treeWidget->resizeColumnToContents(0);
    m_treeWidget->selectAll();
    if (Utils::HostOsInfo::isMacHost())
        m_treeWidget->setAlternatingRowColors(true);
    adjustButtonWidths();
    updateButtons();

    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    connect(m_buttonBox->button(QDialogButtonBox::Save),
            &QAbstractButton::clicked,
            this,
            &SaveItemsDialog::collectItemsToSave);
    connect(discardButton, &QAbstractButton::clicked, this, &SaveItemsDialog::discardAll);
    connect(m_treeWidget, &QTreeWidget::itemSelectionChanged, this, &SaveItemsDialog::updateButtons);
}

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

void SaveItemsDialog::updateButtons()
{
    int count = m_treeWidget->selectedItems().count();
    QPushButton *saveButton = m_buttonBox->button(QDialogButtonBox::Save);
    bool buttonsEnabled = true;
    QString saveText = tr("&Save");
    QString diffText = tr("&Diff && Cancel");
    if (count == m_treeWidget->topLevelItemCount()) {
        saveText = tr("&Save All");
        diffText = tr("&Diff All && Cancel");
    } else if (count == 0) {
        buttonsEnabled = false;
    } else {
        saveText = tr("&Save Selected");
        diffText = tr("&Diff Selected && Cancel");
    }
    saveButton->setEnabled(buttonsEnabled);
    saveButton->setText(saveText);
    if (m_diffButton) {
        m_diffButton->setEnabled(buttonsEnabled);
        m_diffButton->setText(diffText);
    }
}

void SaveItemsDialog::adjustButtonWidths()
{
    // give save button a size that all texts fit in, so it doesn't get resized
    // Mac: make cancel + save button same size (work around dialog button box issue)
    QStringList possibleTexts;
    possibleTexts << tr("Save") << tr("Save All");
    if (m_treeWidget->topLevelItemCount() > 1)
        possibleTexts << tr("Save Selected");
    int maxTextWidth = 0;
    QPushButton *saveButton = m_buttonBox->button(QDialogButtonBox::Save);
    for (const QString &text : qAsConst(possibleTexts)) {
        saveButton->setText(text);
        int hint = saveButton->sizeHint().width();
        if (hint > maxTextWidth)
            maxTextWidth = hint;
    }
    if (Utils::HostOsInfo::isMacHost()) {
        QPushButton *cancelButton = m_buttonBox->button(QDialogButtonBox::Cancel);
        int cancelButtonWidth = cancelButton->sizeHint().width();
        if (cancelButtonWidth > maxTextWidth)
            maxTextWidth = cancelButtonWidth;
        cancelButton->setMinimumWidth(maxTextWidth);
    }
    saveButton->setMinimumWidth(maxTextWidth);
}

void SaveItemsDialog::collectItemsToSave()
{
    m_itemsToSave.clear();
    const QList<QTreeWidgetItem *> items = m_treeWidget->selectedItems();
    for (const QTreeWidgetItem *item : items) {
        m_itemsToSave.append(item->data(0, Qt::UserRole).value<IDocument*>());
    }
    accept();
}

void SaveItemsDialog::collectFilesToDiff()
{
    m_filesToDiff.clear();
    const QList<QTreeWidgetItem *> items = m_treeWidget->selectedItems();
    for (const QTreeWidgetItem *item : items) {
        if (auto doc = item->data(0, Qt::UserRole).value<IDocument*>())
            m_filesToDiff.append(doc->filePath().toString());
    }
    reject();
}

void SaveItemsDialog::discardAll()
{
    m_treeWidget->clearSelection();
    collectItemsToSave();
}

QList<IDocument*> SaveItemsDialog::itemsToSave() const
{
    return m_itemsToSave;
}

QStringList SaveItemsDialog::filesToDiff() const
{
    return m_filesToDiff;
}

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

bool SaveItemsDialog::alwaysSaveChecked()
{
    return m_saveBeforeBuildCheckBox->isChecked();
}