Clean up ListDevices and, in the process, also MainWindow and the KCM.

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1089088
This commit is contained in:
Volker Lanz 2010-02-12 11:29:12 +00:00
parent 921d6b18e6
commit 7991de7fef
9 changed files with 98 additions and 81 deletions

View File

@ -19,8 +19,6 @@
#include "gui/listdevices.h"
#include "gui/partitionmanagerwidget.h"
#include "core/device.h"
#include "util/globallog.h"
@ -29,41 +27,47 @@
#include <kmenu.h>
#include <kactioncollection.h>
class ListDeviceWidgetItem : public QListWidgetItem
{
public:
ListDeviceWidgetItem(const Device& d) :
QListWidgetItem(DesktopIcon(d.iconName()), d.deviceNode() + " (" + Capacity(d).toString() + ')'),
deviceNode(d.deviceNode())
{
setToolTip(d.deviceNode() + " (" + Capacity(d).toString() + ", " + d.name() + ')');
setSizeHint(QSize(0, 32));
}
const QString deviceNode;
};
/** Creates a new ListDevices instance.
@param parent the parent widget
*/
ListDevices::ListDevices(QWidget* parent) :
QWidget(parent),
Ui::ListDevicesBase(),
m_ActionCollection(NULL),
m_PartitionManagerWidget(NULL)
m_ActionCollection(NULL)
{
setupUi(this);
}
void ListDevices::updateDevices()
void ListDevices::updateDevices(OperationStack::Devices& devices, Device* selected_device)
{
int idx = listDevices().currentRow();
listDevices().clear();
foreach(const Device* d, pmWidget().previewDevices())
{
const QString shortText = d->deviceNode() + " (" + Capacity(*d).toString() + ')';
const QString longText = d->deviceNode() + " (" + Capacity(*d).toString() + ", " + d->name() + ')';
QListWidgetItem* item = new QListWidgetItem(DesktopIcon(d->iconName()), shortText);
item->setToolTip(longText);
item->setSizeHint(QSize(0, 32));
listDevices().addItem(item);
}
foreach(const Device* d, devices)
listDevices().addItem(new ListDeviceWidgetItem(*d));
if (idx > -1 && idx < listDevices().count())
listDevices().setCurrentRow(idx);
if (pmWidget().selectedDevice())
if (selected_device)
{
for (idx = 0; idx < pmWidget().previewDevices().size(); idx++)
if (pmWidget().previewDevices()[idx] == pmWidget().selectedDevice())
for (idx = 0; idx < devices.size(); idx++)
if (devices[idx] == selected_device)
{
listDevices().setCurrentRow(idx);
break;
@ -73,23 +77,21 @@ void ListDevices::updateDevices()
void ListDevices::on_m_ListDevices_itemSelectionChanged()
{
int idx = -1;
if (listDevices().selectedItems().size() == 1)
idx = listDevices().row(listDevices().selectedItems()[0]);
{
ListDeviceWidgetItem* item = dynamic_cast<ListDeviceWidgetItem*>(listDevices().selectedItems()[0]);
Device* d = NULL;
if (idx >= 0 && idx < pmWidget().previewDevices().size())
d = pmWidget().previewDevices()[idx];
emit selectionChanged(d);
if (item != NULL)
emit selectionChanged(item->deviceNode);
}
}
void ListDevices::on_m_ListDevices_customContextMenuRequested(const QPoint& pos)
{
Q_ASSERT(actionCollection());
KMenu deviceMenu;
deviceMenu.addAction(actionCollection()->action("createNewPartitionTable"));
deviceMenu.exec(listDevices().viewport()->mapToGlobal(pos));
if (actionCollection() && actionCollection()->action("createNewPartitionTable"))
{
KMenu deviceMenu;
deviceMenu.addAction(actionCollection()->action("createNewPartitionTable"));
deviceMenu.exec(listDevices().viewport()->mapToGlobal(pos));
}
}

View File

@ -23,15 +23,14 @@
#include "util/libpartitionmanagerexport.h"
#include "core/operationstack.h"
#include "ui_listdevicesbase.h"
#include <QWidget>
#include <kdebug.h>
class Device;
class QPoint;
class PartitionManagerWidget;
class KActionCollection;
/** @brief A list of devices.
@ -46,21 +45,17 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ListDevices : public QWidget, public Ui:
ListDevices(QWidget* parent);
signals:
void selectionChanged(Device*);
void selectionChanged(const QString& device_node);
public:
void init(KActionCollection* coll, PartitionManagerWidget* pm_widget) { m_ActionCollection = coll; m_PartitionManagerWidget = pm_widget; }
void setActionCollection(KActionCollection* coll) { m_ActionCollection = coll; }
public slots:
void updateDevices();
void updateDevices(OperationStack::Devices& devices, Device* selected_device);
protected:
QListWidget& listDevices() { Q_ASSERT(m_ListDevices); return *m_ListDevices; }
const QListWidget& listDevices() const { Q_ASSERT(m_ListDevices); return *m_ListDevices; }
PartitionManagerWidget& pmWidget() { Q_ASSERT(m_PartitionManagerWidget); return *m_PartitionManagerWidget; }
const PartitionManagerWidget& pmWidget() const { Q_ASSERT(m_PartitionManagerWidget); return *m_PartitionManagerWidget; }
KActionCollection* actionCollection() { return m_ActionCollection; }
protected slots:
@ -69,7 +64,6 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ListDevices : public QWidget, public Ui:
private:
KActionCollection* m_ActionCollection;
PartitionManagerWidget* m_PartitionManagerWidget;
};
#endif

View File

@ -49,6 +49,9 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ListOperations : public QWidget, public
public:
void init(KActionCollection* coll, PartitionManagerWidget* pm_widget) { m_ActionCollection = coll; m_PartitionManagerWidget = pm_widget; }
public slots:
void updateOperations();
protected:
KActionCollection* actionCollection() { return m_ActionCollection; }
@ -60,7 +63,6 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT ListOperations : public QWidget, public
protected slots:
void on_m_ListOperations_customContextMenuRequested(const QPoint& pos);
void updateOperations();
private:
KActionCollection* m_ActionCollection;

View File

@ -20,6 +20,7 @@
#include "gui/mainwindow.h"
#include "gui/infopane.h"
#include "gui/applyprogressdialog.h"
#include "gui/scanprogressdialog.h"
#include "core/device.h"
@ -61,7 +62,7 @@ void MainWindow::init()
setupStatusBar();
setupConnections();
listDevices().init(actionCollection(), &pmWidget());
listDevices().setActionCollection(actionCollection());
listOperations().init(actionCollection(), &pmWidget());
pmWidget().init(actionCollection(), "partitionmanagerrc");
@ -76,8 +77,7 @@ void MainWindow::init()
dockInformation().setWidget(&infoPane());
// trigger an update for the info pane so it can re-layout itself
updateSelection(NULL);
infoPane().clear();
}
void MainWindow::closeEvent(QCloseEvent* event)
@ -109,10 +109,21 @@ void MainWindow::closeEvent(QCloseEvent* event)
void MainWindow::changeEvent(QEvent* event)
{
if ((event->type() == QEvent::ActivationChange || event->type() == QEvent::WindowStateChange) && event->spontaneous() && isActiveWindow() && pmWidget().applyProgressDialog().isVisible())
if ((event->type() == QEvent::ActivationChange || event->type() == QEvent::WindowStateChange) && event->spontaneous() && isActiveWindow())
{
pmWidget().applyProgressDialog().activateWindow();
pmWidget().applyProgressDialog().raise();
QWidget* w = NULL;
if (pmWidget().applyProgressDialog().isVisible())
w = &pmWidget().applyProgressDialog();
else if (pmWidget().scanProgressDialog().isVisible())
w = &pmWidget().scanProgressDialog();
if (w != NULL)
{
w->activateWindow();
w->raise();
w->setFocus();
}
}
KXmlGuiWindow::changeEvent(event);
@ -132,17 +143,12 @@ void MainWindow::setupActions()
void MainWindow::setupConnections()
{
connect(&pmWidget(), SIGNAL(devicesChanged()), SLOT(updateDevices()));
connect(&pmWidget(), SIGNAL(operationsChanged()), &listOperations(), SLOT(updateOperations()));
connect(&pmWidget(), SIGNAL(operationsChanged()), SLOT(updateStatusBar()));
connect(&pmWidget(), SIGNAL(selectedPartitionChanged(const Partition*)), SLOT(updateSelection(const Partition*)));
connect(&dockInformation(), SIGNAL(dockLocationChanged(Qt::DockWidgetArea)), SLOT(onDockLocationChanged(Qt::DockWidgetArea)));
connect(&listDevices(), SIGNAL(selectionChanged(const QString&)), &pmWidget(), SLOT(setSelectedDevice(const QString&)));
}
void MainWindow::setupStatusBar()
{
statusBar()->addWidget(&statusText());
updateStatusBar();
}
void MainWindow::loadConfig()
@ -161,14 +167,16 @@ void MainWindow::saveConfig() const
Config::self()->writeConfig();
}
void MainWindow::updateStatusBar()
void MainWindow::on_m_PartitionManagerWidget_operationsChanged()
{
listOperations().updateOperations();
statusText().setText(i18ncp("@info:status", "One pending operation", "%1 pending operations", pmWidget().numPendingOperations()));
}
void MainWindow::updateDevices()
void MainWindow::on_m_PartitionManagerWidget_devicesChanged()
{
listDevices().updateDevices();
listDevices().updateDevices(pmWidget().previewDevices(), pmWidget().selectedDevice());
if (pmWidget().selectedDevice())
infoPane().showDevice(dockWidgetArea(&dockInformation()), *pmWidget().selectedDevice());
@ -178,15 +186,9 @@ void MainWindow::updateDevices()
updateWindowTitle();
}
void MainWindow::on_m_ListDevices_selectionChanged(Device* d)
void MainWindow::on_m_DockInformation_dockLocationChanged(Qt::DockWidgetArea)
{
pmWidget().setSelectedDevice(d);
updateSelection(NULL);
}
void MainWindow::onDockLocationChanged(Qt::DockWidgetArea)
{
updateSelection(pmWidget().selectedPartition());
on_m_PartitionManagerWidget_selectedPartitionChanged(pmWidget().selectedPartition());
}
void MainWindow::updateWindowTitle()
@ -201,7 +203,7 @@ void MainWindow::updateWindowTitle()
setWindowTitle(title);
}
void MainWindow::updateSelection(const Partition* p)
void MainWindow::on_m_PartitionManagerWidget_selectedPartitionChanged(const Partition* p)
{
if (p)
infoPane().showPartition(dockWidgetArea(&dockInformation()), *p);

View File

@ -87,17 +87,15 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT MainWindow : public KXmlGuiWindow, publi
const QLabel& statusText() const { Q_ASSERT(m_StatusText); return *m_StatusText; }
protected slots:
void on_m_ListDevices_selectionChanged(Device* d);
void onDockLocationChanged(Qt::DockWidgetArea area);
void closeEvent(QCloseEvent*);
void changeEvent(QEvent* event);
void init();
void updateDevices();
void updateStatusBar();
void updateSelection(const Partition* p);
void on_m_DockInformation_dockLocationChanged(Qt::DockWidgetArea);
void on_m_PartitionManagerWidget_devicesChanged();
void on_m_PartitionManagerWidget_operationsChanged();
void on_m_PartitionManagerWidget_selectedPartitionChanged(const Partition* p);
private:
QLabel* m_StatusText;

View File

@ -396,6 +396,18 @@ void PartitionManagerWidget::clearSelectedPartition()
updatePartitions();
}
void PartitionManagerWidget::setSelectedDevice(const QString& device_node)
{
foreach(Device* d, previewDevices())
if (d->deviceNode() == device_node)
{
setSelectedDevice(d);
return;
}
setSelectedDevice(NULL);
}
void PartitionManagerWidget::setSelectedDevice(Device* d)
{
m_SelectedDevice = d;

View File

@ -59,6 +59,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT PartitionManagerWidget : public QWidget,
public slots:
void setSelectedDevice(Device* d);
void setSelectedDevice(const QString& device_node);
public:
void init(KActionCollection* coll, const QString& config_name);

View File

@ -59,7 +59,7 @@ PartitionManagerKCM::PartitionManagerKCM(QWidget* parent, const QVariantList&) :
setButtons(Apply);
setupConnections();
listDevices().init(actionCollection(), &pmWidget());
listDevices().setActionCollection(actionCollection());
listOperations().init(actionCollection(), &pmWidget());
pmWidget().init(actionCollection(), "kcm_partitionmanagerrc");
@ -104,17 +104,21 @@ void PartitionManagerKCM::onNewLogMessage(Log::Level, const QString& s)
void PartitionManagerKCM::setupConnections()
{
connect(&pmWidget(), SIGNAL(devicesChanged()), &listDevices(), SLOT(updateDevices()));
connect(&pmWidget(), SIGNAL(operationsChanged()), &listOperations(), SLOT(updateOperations()));
connect(&listDevices(), SIGNAL(selectionChanged(Device*)), &pmWidget(), SLOT(setSelectedDevice(Device*)));
connect(&pmWidget(), SIGNAL(operationsChanged()), SLOT(onStatusChanged()));
connect(&listDevices(), SIGNAL(selectionChanged(const QString&)), &pmWidget(), SLOT(setSelectedDevice(const QString&)));
}
void PartitionManagerKCM::onStatusChanged()
void PartitionManagerKCM::on_m_PartitionManagerWidget_operationsChanged()
{
listOperations().updateOperations();
emit changed(pmWidget().numPendingOperations() > 0);
}
void PartitionManagerKCM::on_m_PartitionManagerWidget_devicesChanged()
{
listDevices().updateDevices(pmWidget().previewDevices(), pmWidget().selectedDevice());
}
void PartitionManagerKCM::setupKCMWorkaround()
{
// The Partition Manager kcm must be run as root, for obvious reasons. system-settings will
@ -145,6 +149,6 @@ void PartitionManagerKCM::onApplyClicked()
if (pmWidget().numPendingOperations() > 0)
actionCollection()->action("applyAllOperations")->trigger();
QTimer::singleShot(0, this, SLOT(onStatusChanged()));
QTimer::singleShot(0, this, SLOT(on_m_PartitionManagerWidget_operationsChanged()));
}

View File

@ -62,9 +62,11 @@ class PartitionManagerKCM : public KCModule, public Ui::PartitionManagerKCMBase
protected slots:
void onNewLogMessage(Log::Level logLevel, const QString& s);
void onStatusChanged();
void onApplyClicked();
void on_m_PartitionManagerWidget_devicesChanged();
void on_m_PartitionManagerWidget_operationsChanged();
private:
KActionCollection* m_ActionCollection;
};