2015-02-07 00:32:25 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008, 2010 by Volker Lanz <vl@fidra.de> *
|
|
|
|
* *
|
|
|
|
* 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
|
|
|
|
2015-06-04 01:51:56 +01:00
|
|
|
#include <kpmcore/core/partitiontable.h>
|
2008-09-24 20:32:01 +01:00
|
|
|
|
2015-07-08 17:16:03 +01:00
|
|
|
#include <kpmcore/gui/partwidget.h>
|
|
|
|
|
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),
|
|
|
|
m_PartitionTable(NULL),
|
|
|
|
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.
|
2015-07-13 15:17:41 +01:00
|
|
|
@param ptable pointer to the PartitionTable to show. Must not be NULL.
|
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;
|
|
|
|
|
|
|
|
if (partitionTable() != NULL) {
|
|
|
|
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());
|
|
|
|
MainWindow* mw = nullptr;
|
|
|
|
foreach( QWidget* widget, qApp->topLevelWidgets() )
|
|
|
|
{
|
|
|
|
mw = qobject_cast< MainWindow* >( widget );
|
|
|
|
if ( mw )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( mw )
|
|
|
|
{
|
|
|
|
connect( mw, &MainWindow::settingsChanged,
|
|
|
|
this, [w]
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
return NULL;
|
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
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
return NULL;
|
2010-04-12 15:35:02 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 20:32:01 +01:00
|
|
|
/** Sets a widget active.
|
2015-07-13 15:17:41 +01:00
|
|
|
@param p pointer to the PartWidget to set active. May be NULL.
|
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
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (p != NULL)
|
|
|
|
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.
|
2015-07-13 15:17:41 +01:00
|
|
|
@param p pointer to the Partition whose widget is to be set active. May be NULL.
|
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
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
setActiveWidget(NULL);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clears the PartTableWidget.
|
|
|
|
*/
|
|
|
|
void PartTableWidget::clear()
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
setActiveWidget(NULL);
|
|
|
|
m_PartitionTable = NULL;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
p->setParent(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartTableWidget::resizeEvent(QResizeEvent*)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
if (partitionTable() == NULL || childWidgets().isEmpty())
|
|
|
|
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
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (child != NULL)
|
|
|
|
emit itemDoubleClicked(child);
|
2008-09-24 20:32:01 +01:00
|
|
|
}
|