2015-02-07 00:32:25 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008, 2010 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 18:33:40 +00:00
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-02-07 00:32:25 +00:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
2008-09-24 20:32:01 +01:00
|
|
|
|
|
|
|
#include "gui/parttablewidget.h"
|
2015-07-17 12:37:30 +01:00
|
|
|
#include "util/guihelpers.h"
|
|
|
|
#include "mainwindow.h"
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2016-01-12 00:12:49 +00:00
|
|
|
#include <core/partitiontable.h>
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2016-01-12 00:12:49 +00:00
|
|
|
#include <gui/partwidget.h>
|
2015-07-08 17:16:03 +01:00
|
|
|
|
2015-07-17 12:37:30 +01:00
|
|
|
#include <QApplication>
|
2008-09-24 20:32:01 +01:00
|
|
|
#include <QMouseEvent>
|
|
|
|
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <KLocalizedString>
|
2008-09-24 20:32:01 +01:00
|
|
|
|
|
|
|
/** Creates a new PartTableWidget.
|
2015-07-13 15:17:41 +01:00
|
|
|
@param parent pointer to the parent widget
|
2008-09-24 20:32:01 +01:00
|
|
|
*/
|
|
|
|
PartTableWidget::PartTableWidget(QWidget* parent) :
|
2015-07-13 15:17:41 +01:00
|
|
|
PartWidgetBase(parent),
|
2016-04-14 19:18:36 +01:00
|
|
|
m_PartitionTable(nullptr),
|
2015-07-13 15:17:41 +01:00
|
|
|
m_LabelEmpty(i18nc("@info", "Please select a device."), this),
|
|
|
|
m_ReadOnly(false)
|
2008-09-24 20:32:01 +01:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
labelEmpty().setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the PartitionTable this widget shows.
|
2016-04-14 19:18:36 +01:00
|
|
|
@param ptable pointer to the PartitionTable to show. Must not be nullptr.
|
2008-09-24 20:32:01 +01:00
|
|
|
*/
|
|
|
|
void PartTableWidget::setPartitionTable(const PartitionTable* ptable)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
clear();
|
|
|
|
|
|
|
|
m_PartitionTable = ptable;
|
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (partitionTable() != nullptr) {
|
2015-07-13 15:17:41 +01:00
|
|
|
foreach(const Partition * p, partitionTable()->children()) {
|
2015-07-17 12:37:30 +01:00
|
|
|
PartWidget* w = new PartWidget(this, p);
|
2015-07-13 15:17:41 +01:00
|
|
|
w->setVisible(true);
|
2015-07-17 12:37:30 +01:00
|
|
|
w->setFileSystemColorCode(GuiHelpers::fileSystemColorCodesFromSettings());
|
2015-07-13 15:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childWidgets().isEmpty()) {
|
|
|
|
labelEmpty().setVisible(true);
|
|
|
|
labelEmpty().setText(i18nc("@info", "No valid partition table was found on this device."));
|
|
|
|
labelEmpty().resize(size());
|
|
|
|
} else {
|
|
|
|
labelEmpty().setVisible(false);
|
|
|
|
positionChildren(this, partitionTable()->children(), childWidgets());
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 15:35:02 +01:00
|
|
|
PartWidget* PartTableWidget::activeWidget()
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
foreach(PartWidget * pw, findChildren<PartWidget*>())
|
|
|
|
if (pw->isActive())
|
|
|
|
return pw;
|
2010-04-12 15:35:02 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
return nullptr;
|
2010-04-12 15:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const PartWidget* PartTableWidget::activeWidget() const
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
foreach(const PartWidget * pw, findChildren<PartWidget*>())
|
|
|
|
if (pw->isActive())
|
|
|
|
return pw;
|
2010-04-12 15:35:02 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
return nullptr;
|
2010-04-12 15:35:02 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 20:32:01 +01:00
|
|
|
/** Sets a widget active.
|
2016-04-14 19:18:36 +01:00
|
|
|
@param p pointer to the PartWidget to set active. May be nullptr.
|
2008-09-24 20:32:01 +01:00
|
|
|
*/
|
|
|
|
void PartTableWidget::setActiveWidget(PartWidget* p)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
if (isReadOnly() || p == activeWidget())
|
|
|
|
return;
|
2010-02-17 10:06:23 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (activeWidget())
|
|
|
|
activeWidget()->setActive(false);
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (p != nullptr)
|
2015-07-13 15:17:41 +01:00
|
|
|
p->setActive(true);
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
emit itemSelectionChanged(p);
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
update();
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets a widget for the given Partition active.
|
2016-04-14 19:18:36 +01:00
|
|
|
@param p pointer to the Partition whose widget is to be set active. May be nullptr.
|
2008-09-24 20:32:01 +01:00
|
|
|
*/
|
|
|
|
void PartTableWidget::setActivePartition(const Partition* p)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
if (isReadOnly())
|
|
|
|
return;
|
2010-02-17 10:06:23 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
foreach(PartWidget * pw, findChildren<PartWidget*>())
|
|
|
|
if (pw->partition() == p) {
|
|
|
|
setActiveWidget(pw);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
setActiveWidget(nullptr);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clears the PartTableWidget.
|
|
|
|
*/
|
|
|
|
void PartTableWidget::clear()
|
|
|
|
{
|
2016-04-14 19:18:36 +01:00
|
|
|
setActiveWidget(nullptr);
|
|
|
|
m_PartitionTable = nullptr;
|
2015-07-13 15:17:41 +01:00
|
|
|
|
|
|
|
// we might have been invoked indirectly via a widget's context menu, so
|
|
|
|
// that its event handler is currently running. therefore, do not delete
|
|
|
|
// the part widgets here but schedule them for deletion once the app
|
|
|
|
// returns to the main loop (and the event handler has finished).
|
|
|
|
foreach(PartWidget * p, childWidgets()) {
|
|
|
|
p->setVisible(false);
|
|
|
|
p->deleteLater();
|
2016-04-14 19:18:36 +01:00
|
|
|
p->setParent(nullptr);
|
2015-07-13 15:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartTableWidget::resizeEvent(QResizeEvent*)
|
|
|
|
{
|
2016-04-14 19:18:36 +01:00
|
|
|
if (partitionTable() == nullptr || childWidgets().isEmpty())
|
2015-07-13 15:17:41 +01:00
|
|
|
labelEmpty().resize(size());
|
|
|
|
else
|
|
|
|
positionChildren(this, partitionTable()->children(), childWidgets());
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartTableWidget::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
if (isReadOnly())
|
|
|
|
return;
|
2010-02-17 10:06:23 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
event->accept();
|
|
|
|
PartWidget* child = qobject_cast<PartWidget*>(childAt(event->pos()));
|
|
|
|
setActiveWidget(child);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartTableWidget::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
if (isReadOnly() || event->button() != Qt::LeftButton)
|
|
|
|
return;
|
2009-01-09 07:10:59 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
event->accept();
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
const PartWidget* child = static_cast<PartWidget*>(childAt(event->pos()));
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (child != nullptr)
|
2015-07-13 15:17:41 +01:00
|
|
|
emit itemDoubleClicked(child);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|