2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-09-16 12:26:28 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-09-16 12:26:28 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-09-16 12:26:28 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-09-16 12:26:28 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-09-16 12:26:28 +02:00
|
|
|
|
|
|
|
|
#include "sidebarwidget.h"
|
|
|
|
|
#include "sidebar.h"
|
|
|
|
|
#include "navigationsubwidget.h"
|
|
|
|
|
|
2014-06-16 18:25:52 +04:00
|
|
|
#include <utils/algorithm.h>
|
2016-08-03 17:55:54 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2010-09-16 12:26:28 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QToolBar>
|
|
|
|
|
#include <QToolButton>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QVBoxLayout>
|
2010-09-16 12:26:28 +02:00
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class SideBarComboBox : public CommandComboBox
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum DataRoles {
|
|
|
|
|
IdRole = Qt::UserRole
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit SideBarComboBox(SideBarWidget *sideBarWidget) : m_sideBarWidget(sideBarWidget) {}
|
|
|
|
|
|
|
|
|
|
private:
|
2018-07-11 07:31:38 +02:00
|
|
|
const Command *command(const QString &text) const override
|
2010-09-16 12:26:28 +02:00
|
|
|
{ return m_sideBarWidget->command(text); }
|
|
|
|
|
|
|
|
|
|
SideBarWidget *m_sideBarWidget;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
|
2018-07-21 21:11:46 +02:00
|
|
|
: m_sideBar(sideBar)
|
2010-09-16 12:26:28 +02:00
|
|
|
{
|
|
|
|
|
m_comboBox = new SideBarComboBox(this);
|
|
|
|
|
m_comboBox->setMinimumContentsLength(15);
|
|
|
|
|
|
|
|
|
|
m_toolbar = new QToolBar(this);
|
|
|
|
|
m_toolbar->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
m_toolbar->addWidget(m_comboBox);
|
|
|
|
|
|
|
|
|
|
QWidget *spacerItem = new QWidget(this);
|
|
|
|
|
spacerItem->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
|
|
|
m_toolbar->addWidget(spacerItem);
|
|
|
|
|
|
|
|
|
|
m_splitAction = new QAction(tr("Split"), m_toolbar);
|
|
|
|
|
m_splitAction->setToolTip(tr("Split"));
|
2016-08-03 17:55:54 +02:00
|
|
|
m_splitAction->setIcon(Utils::Icons::SPLIT_HORIZONTAL_TOOLBAR.icon());
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_splitAction, &QAction::triggered, this, &SideBarWidget::splitMe);
|
2010-09-16 12:26:28 +02:00
|
|
|
m_toolbar->addAction(m_splitAction);
|
|
|
|
|
|
2015-01-19 17:07:49 +01:00
|
|
|
m_closeAction = new QAction(tr("Close"), m_toolbar);
|
|
|
|
|
m_closeAction->setToolTip(tr("Close"));
|
2016-08-03 17:55:54 +02:00
|
|
|
m_closeAction->setIcon(Utils::Icons::CLOSE_SPLIT_BOTTOM.icon());
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_closeAction, &QAction::triggered, this, &SideBarWidget::closeMe);
|
2015-01-19 17:07:49 +01:00
|
|
|
m_toolbar->addAction(m_closeAction);
|
2010-09-16 12:26:28 +02:00
|
|
|
|
2018-07-21 21:11:46 +02:00
|
|
|
auto lay = new QVBoxLayout();
|
2010-09-16 12:26:28 +02:00
|
|
|
lay->setMargin(0);
|
|
|
|
|
lay->setSpacing(0);
|
|
|
|
|
setLayout(lay);
|
|
|
|
|
lay->addWidget(m_toolbar);
|
|
|
|
|
|
|
|
|
|
QStringList titleList = m_sideBar->availableItemTitles();
|
2014-06-16 18:25:52 +04:00
|
|
|
Utils::sort(titleList);
|
2010-09-16 12:26:28 +02:00
|
|
|
QString t = id;
|
|
|
|
|
if (titleList.count()) {
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const QString &itemTitle, titleList)
|
2010-09-16 12:26:28 +02:00
|
|
|
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
|
|
|
|
|
|
|
|
|
|
m_comboBox->setCurrentIndex(0);
|
|
|
|
|
if (t.isEmpty())
|
|
|
|
|
t = m_comboBox->itemData(0, SideBarComboBox::IdRole).toString();
|
|
|
|
|
}
|
|
|
|
|
setCurrentItem(t);
|
|
|
|
|
|
2016-02-02 09:10:54 +02:00
|
|
|
connect(m_comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
|
this, &SideBarWidget::setCurrentIndex);
|
2010-09-16 12:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-21 21:11:46 +02:00
|
|
|
SideBarWidget::~SideBarWidget() = default;
|
2010-09-16 12:26:28 +02:00
|
|
|
|
|
|
|
|
QString SideBarWidget::currentItemTitle() const
|
|
|
|
|
{
|
|
|
|
|
return m_comboBox->currentText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SideBarWidget::currentItemId() const
|
|
|
|
|
{
|
|
|
|
|
if (m_currentItem)
|
|
|
|
|
return m_currentItem->id();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBarWidget::setCurrentItem(const QString &id)
|
|
|
|
|
{
|
|
|
|
|
if (!id.isEmpty()) {
|
|
|
|
|
int idx = m_comboBox->findData(QVariant(id), SideBarComboBox::IdRole);
|
|
|
|
|
|
|
|
|
|
if (idx < 0)
|
|
|
|
|
idx = 0;
|
|
|
|
|
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(m_comboBox);
|
2010-09-16 12:26:28 +02:00
|
|
|
m_comboBox->setCurrentIndex(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SideBarItem *item = m_sideBar->item(id);
|
|
|
|
|
if (!item)
|
|
|
|
|
return;
|
|
|
|
|
removeCurrentItem();
|
|
|
|
|
m_currentItem = item;
|
|
|
|
|
|
|
|
|
|
layout()->addWidget(m_currentItem->widget());
|
|
|
|
|
m_currentItem->widget()->show();
|
|
|
|
|
|
|
|
|
|
// Add buttons and remember their actions for later removal
|
|
|
|
|
foreach (QToolButton *b, m_currentItem->createToolBarWidgets())
|
|
|
|
|
m_addedToolBarActions.append(m_toolbar->insertWidget(m_splitAction, b));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBarWidget::updateAvailableItems()
|
|
|
|
|
{
|
2017-09-30 07:12:57 +02:00
|
|
|
QSignalBlocker blocker(m_comboBox);
|
2010-09-16 12:26:28 +02:00
|
|
|
QString currentTitle = m_comboBox->currentText();
|
|
|
|
|
m_comboBox->clear();
|
|
|
|
|
QStringList titleList = m_sideBar->availableItemTitles();
|
|
|
|
|
if (!currentTitle.isEmpty() && !titleList.contains(currentTitle))
|
|
|
|
|
titleList.append(currentTitle);
|
2014-06-16 18:25:52 +04:00
|
|
|
Utils::sort(titleList);
|
2010-09-16 12:26:28 +02:00
|
|
|
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const QString &itemTitle, titleList)
|
2010-09-16 12:26:28 +02:00
|
|
|
m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
|
|
|
|
|
|
|
|
|
|
int idx = m_comboBox->findText(currentTitle);
|
|
|
|
|
|
|
|
|
|
if (idx < 0)
|
|
|
|
|
idx = 0;
|
|
|
|
|
|
|
|
|
|
m_comboBox->setCurrentIndex(idx);
|
|
|
|
|
m_splitAction->setEnabled(titleList.count() > 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBarWidget::removeCurrentItem()
|
|
|
|
|
{
|
|
|
|
|
if (!m_currentItem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QWidget *w = m_currentItem->widget();
|
|
|
|
|
w->hide();
|
|
|
|
|
layout()->removeWidget(w);
|
2018-07-21 21:11:46 +02:00
|
|
|
w->setParent(nullptr);
|
2010-09-16 12:26:28 +02:00
|
|
|
m_sideBar->makeItemAvailable(m_currentItem);
|
|
|
|
|
|
|
|
|
|
// Delete custom toolbar widgets
|
|
|
|
|
qDeleteAll(m_addedToolBarActions);
|
|
|
|
|
m_addedToolBarActions.clear();
|
|
|
|
|
|
2018-07-21 21:11:46 +02:00
|
|
|
m_currentItem = nullptr;
|
2010-09-16 12:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBarWidget::setCurrentIndex(int)
|
|
|
|
|
{
|
|
|
|
|
setCurrentItem(m_comboBox->itemData(m_comboBox->currentIndex(),
|
|
|
|
|
SideBarComboBox::IdRole).toString());
|
|
|
|
|
emit currentWidgetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-16 10:52:41 +02:00
|
|
|
Command *SideBarWidget::command(const QString &title) const
|
2010-09-16 12:26:28 +02:00
|
|
|
{
|
2014-10-09 16:03:27 +02:00
|
|
|
const QString id = m_sideBar->idForTitle(title);
|
|
|
|
|
if (id.isEmpty())
|
2018-07-21 21:11:46 +02:00
|
|
|
return nullptr;
|
2014-11-16 10:52:41 +02:00
|
|
|
const QMap<QString, Command*> shortcutMap = m_sideBar->shortcutMap();
|
|
|
|
|
QMap<QString, Command*>::const_iterator r = shortcutMap.find(id);
|
2010-09-16 12:26:28 +02:00
|
|
|
if (r != shortcutMap.end())
|
|
|
|
|
return r.value();
|
2018-07-21 21:11:46 +02:00
|
|
|
return nullptr;
|
2010-09-16 12:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-19 17:07:49 +01:00
|
|
|
void SideBarWidget::setCloseIcon(const QIcon &icon)
|
|
|
|
|
{
|
|
|
|
|
m_closeAction->setIcon(icon);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Core
|