2017-01-17 12:33:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppminimizableinfobars.h"
|
|
|
|
|
|
|
|
|
|
#include <QToolButton>
|
|
|
|
|
|
2022-05-03 11:01:24 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <utils/infobar.h>
|
2017-01-17 12:33:55 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2017-01-17 12:33:55 +01:00
|
|
|
|
2022-05-03 12:18:16 +02:00
|
|
|
const char SETTINGS_PREFIX[] = "ShowInfoBarFor";
|
2022-05-03 11:01:24 +02:00
|
|
|
const bool kShowInInfoBarDefault = true;
|
2022-05-02 17:39:35 +02:00
|
|
|
|
2022-05-03 11:01:24 +02:00
|
|
|
using namespace Core;
|
2020-06-17 12:23:44 +02:00
|
|
|
using namespace Utils;
|
2017-01-17 12:33:55 +01:00
|
|
|
|
|
|
|
|
namespace CppEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2022-05-03 11:01:24 +02:00
|
|
|
MinimizableInfoBars::MinimizableInfoBars(InfoBar &infoBar)
|
|
|
|
|
: m_infoBar(infoBar)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2022-05-04 15:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MinimizableInfoBars::setPossibleInfoBarEntries(const QList<Utils::InfoBarEntry> &entries)
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_actions.isEmpty());
|
|
|
|
|
m_infoEntries.clear();
|
|
|
|
|
m_isInfoVisible.clear();
|
|
|
|
|
for (const Utils::InfoBarEntry &entry : entries) {
|
|
|
|
|
m_infoEntries.insert(entry.id(), entry);
|
|
|
|
|
m_isInfoVisible.insert(entry.id(), false);
|
|
|
|
|
}
|
2022-05-02 17:11:18 +02:00
|
|
|
createActions();
|
2017-01-17 12:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 11:01:24 +02:00
|
|
|
void MinimizableInfoBars::setSettingsGroup(const QString &settingsGroup)
|
|
|
|
|
{
|
|
|
|
|
m_settingsGroup = settingsGroup;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:11:18 +02:00
|
|
|
void MinimizableInfoBars::createActions()
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2022-05-04 15:03:52 +02:00
|
|
|
QTC_CHECK(m_actions.isEmpty());
|
|
|
|
|
for (const Utils::InfoBarEntry &entry : qAsConst(m_infoEntries)) {
|
|
|
|
|
const Id id = entry.id();
|
|
|
|
|
auto action = new QAction(this);
|
|
|
|
|
action->setToolTip(entry.text());
|
|
|
|
|
action->setIcon(Icons::WARNING_TOOLBAR.pixmap());
|
|
|
|
|
connect(action, &QAction::triggered, this, [this, id]() {
|
|
|
|
|
setShowInInfoBar(id, true);
|
|
|
|
|
updateInfo(id);
|
|
|
|
|
});
|
|
|
|
|
action->setVisible(!showInInfoBar(id));
|
|
|
|
|
m_actions.insert(id, action);
|
|
|
|
|
}
|
2022-05-02 17:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:18:16 +02:00
|
|
|
QString MinimizableInfoBars::settingsKey(const Id &id) const
|
2022-05-03 11:01:24 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(!m_settingsGroup.isEmpty());
|
2022-05-03 12:18:16 +02:00
|
|
|
return m_settingsGroup + '/' + SETTINGS_PREFIX + id.toString();
|
2022-05-03 11:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:11:18 +02:00
|
|
|
void MinimizableInfoBars::createShowInfoBarActions(const ActionCreator &actionCreator) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(actionCreator, return );
|
|
|
|
|
|
|
|
|
|
for (QAction *action : m_actions) {
|
|
|
|
|
auto *button = new QToolButton();
|
|
|
|
|
button->setDefaultAction(action);
|
|
|
|
|
QAction *toolbarAction = actionCreator(button);
|
|
|
|
|
connect(action, &QAction::changed, toolbarAction, [action, toolbarAction] {
|
|
|
|
|
toolbarAction->setVisible(action->isVisible());
|
|
|
|
|
});
|
|
|
|
|
toolbarAction->setVisible(action->isVisible());
|
|
|
|
|
}
|
2017-01-17 12:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 15:03:52 +02:00
|
|
|
void MinimizableInfoBars::setInfoVisible(const Id &id, bool visible)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2022-05-04 15:03:52 +02:00
|
|
|
QTC_CHECK(m_isInfoVisible.contains(id));
|
|
|
|
|
m_isInfoVisible.insert(id, visible);
|
|
|
|
|
updateInfo(id);
|
2017-01-17 12:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 15:03:52 +02:00
|
|
|
void MinimizableInfoBars::updateInfo(const Id &id)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
|
|
|
|
m_infoBar.removeInfo(id);
|
|
|
|
|
|
|
|
|
|
bool show = false;
|
2022-05-04 15:03:52 +02:00
|
|
|
if (m_isInfoVisible.value(id)) {
|
2022-05-03 12:18:16 +02:00
|
|
|
if (showInInfoBar(id))
|
2022-05-04 15:03:52 +02:00
|
|
|
showInfoBar(id);
|
2017-01-17 12:33:55 +01:00
|
|
|
else
|
|
|
|
|
show = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:11:18 +02:00
|
|
|
QAction *action = m_actions.value(id);
|
|
|
|
|
if (QTC_GUARD(action))
|
|
|
|
|
action->setVisible(show);
|
2017-01-17 12:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 15:03:52 +02:00
|
|
|
void MinimizableInfoBars::showInfoBar(const Id &id)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2022-05-04 15:03:52 +02:00
|
|
|
const InfoBarEntry entry = m_infoEntries.value(id);
|
|
|
|
|
InfoBarEntry info(entry);
|
2017-01-17 17:43:44 +01:00
|
|
|
info.removeCancelButton();
|
2017-01-17 12:33:55 +01:00
|
|
|
// The minimizer() might delete the "Minimize" button immediately and as
|
|
|
|
|
// result invalid reads will happen in QToolButton::mouseReleaseEvent().
|
|
|
|
|
// Avoid this by running the minimizer in the next event loop iteration.
|
2022-05-04 15:03:52 +02:00
|
|
|
info.addCustomButton(MinimizableInfoBars::tr("Minimize"), [this, id] {
|
2022-05-03 11:01:24 +02:00
|
|
|
QMetaObject::invokeMethod(
|
2022-05-04 15:03:52 +02:00
|
|
|
this,
|
|
|
|
|
[id, this] {
|
|
|
|
|
setShowInInfoBar(id, false);
|
|
|
|
|
updateInfo(id);
|
|
|
|
|
},
|
|
|
|
|
Qt::QueuedConnection);
|
2017-01-17 12:33:55 +01:00
|
|
|
});
|
2022-05-04 15:03:52 +02:00
|
|
|
m_infoBar.addInfo(info);
|
2022-05-03 11:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:18:16 +02:00
|
|
|
bool MinimizableInfoBars::showInInfoBar(const Id &id) const
|
2022-05-03 11:01:24 +02:00
|
|
|
{
|
2022-05-03 12:18:16 +02:00
|
|
|
return ICore::settings()->value(settingsKey(id), kShowInInfoBarDefault).toBool();
|
2022-05-03 11:01:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:18:16 +02:00
|
|
|
void MinimizableInfoBars::setShowInInfoBar(const Id &id, bool show)
|
2022-05-03 11:01:24 +02:00
|
|
|
{
|
2022-05-03 12:18:16 +02:00
|
|
|
ICore::settings()->setValueWithDefault(settingsKey(id), show, kShowInInfoBarDefault);
|
2017-01-17 12:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppEditor
|