Sort the devices in TelldusCenter by its name.

If anyone thinks to breaks their preferred order, please read
http://xkcd.com/1172/
This closes #112.
This commit is contained in:
Micke Prag 2013-02-13 11:10:07 +01:00
parent 475fdb9d59
commit 9a28b1444b
3 changed files with 19 additions and 7 deletions

View file

@ -3,6 +3,7 @@
#include "devicemodel.h"
#include <QHeaderView>
#include <QStyledItemDelegate>
#include <QSortFilterProxyModel>
#include <QDebug>
class MethodDelegate : public QStyledItemDelegate {
@ -21,7 +22,7 @@ DeviceView::DeviceView(QWidget *parent)
setSelectionMode( QAbstractItemView::SingleSelection );
horizontalHeader()->setStretchLastSection( true );
verticalHeader()->hide();
setItemDelegate(new MethodDelegate(this));
setItemDelegate(new MethodDelegate(this));
}
void DeviceView::setModel ( QAbstractItemModel * model ) {
@ -32,7 +33,7 @@ void DeviceView::setModel ( QAbstractItemModel * model ) {
}
void DeviceView::rowsUpdated ( const QModelIndex & /*parent*/, int start, int end ) {
DeviceModel *model = qobject_cast<DeviceModel*>( this->model() );
QSortFilterProxyModel *model = qobject_cast<QSortFilterProxyModel*>( this->model() );
if (!model) {
return;
}
@ -56,10 +57,15 @@ QWidget *MethodDelegate::createEditor(QWidget *parent, const QStyleOptionViewIte
if (!p) {
return 0;
}
DeviceModel *model = qobject_cast<DeviceModel*>( p->model() );
QSortFilterProxyModel *sModel = qobject_cast<QSortFilterProxyModel*>( p->model() );
if (!sModel) {
return 0;
}
DeviceModel *model = qobject_cast<DeviceModel*>( sModel->sourceModel() );
if (!model) {
return 0;
}
MethodWidget *widget = new MethodWidget(model->device(index), parent);
MethodWidget *widget = new MethodWidget(model->device(sModel->mapToSource(index)), parent);
return widget;
}

View file

@ -23,7 +23,11 @@ DeviceWidget::DeviceWidget(QWidget *parent) :
removeToolButton(this),
editToolButton(this)
{
deviceView.setModel( &model );
sortedModel.setSourceModel(&model);
sortedModel.setDynamicSortFilter(true);
sortedModel.setSortCaseSensitivity(Qt::CaseInsensitive);
sortedModel.sort(1, Qt::AscendingOrder);
deviceView.setModel( &sortedModel );
deviceView.resizeColumnsToContents();
deviceView.resizeRowsToContents();
connect( &deviceView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(listActivated(const QModelIndex &)) );
@ -134,7 +138,7 @@ void DeviceWidget::deleteDevice() {
msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
msgBox.setDefaultButton( QMessageBox::No );
if ( msgBox.exec() == QMessageBox::Yes) {
QModelIndex index = deviceView.currentIndex();
QModelIndex index = sortedModel.mapToSource(deviceView.currentIndex());
Device *device = model.device(index);
if (device) {
device->remove();
@ -143,7 +147,7 @@ void DeviceWidget::deleteDevice() {
}
void DeviceWidget::editDevice() {
QModelIndex index = deviceView.currentIndex();
QModelIndex index = sortedModel.mapToSource(deviceView.currentIndex());
Device device( model.deviceId(index), 0 );
QDialog *dialog;

View file

@ -4,6 +4,7 @@
#include <QtGui/QWidget>
#include <QtGui/QTableView>
#include <QtGui/QToolButton>
#include <QtGui/QSortFilterProxyModel>
#include "devicemodel.h"
#include "deviceview.h"
@ -32,6 +33,7 @@ private slots:
private:
DeviceModel model;
QSortFilterProxyModel sortedModel;
DeviceView deviceView;
QToolButton addToolButton, removeToolButton, editToolButton;
};