1 /*
2 pipesmodel.cpp
3
4 Copyright (c) 2007 by Charles Connell <charles@connells.org>
5
6 Kopete (c) 2007 by the Kopete developers <kopete-devel@kde.org>
7
8 *************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 *************************************************************************
16 */
17 #include "pipesmodel.h"
18
19 #include "pipesdelegate.h"
20 #include "pipesplugin.h"
21
22 #include <kglobal.h>
23 #include <kconfiggroup.h>
24 #include <ksharedconfig.h>
25 #include <klocale.h>
26
PipesModel(QObject * parent)27 PipesModel::PipesModel (QObject *parent)
28 : QAbstractTableModel(parent)
29 {
30 }
31
rowCount(const QModelIndex &) const32 int PipesModel::rowCount(const QModelIndex & /* parent */) const
33 {
34 return mPipesList.count();
35 }
36
columnCount(const QModelIndex &) const37 int PipesModel::columnCount(const QModelIndex & /* parent */) const
38 {
39 return PipesDelegate::TotalColumns;
40 }
41
data(const QModelIndex & index,int role) const42 QVariant PipesModel::data(const QModelIndex &index, int role) const
43 {
44 if (!index.isValid()) {
45 return QVariant();
46 }
47
48 PipesPlugin::PipeOptions pipe = mPipesList.value(index.row());
49
50 if (role == Qt::TextAlignmentRole) {
51 return (int)(Qt::AlignLeft | Qt::AlignVCenter);
52 } else if (role == Qt::CheckStateRole) {
53 if (index.column() == PipesDelegate::EnabledColumn) {
54 return pipe.enabled ? Qt::Checked : Qt::Unchecked;
55 }
56 } else if (role == Qt::DisplayRole) {
57 if (!pipe.uid.isNull()) {
58 if (index.column() == PipesDelegate::PathColumn) {
59 return pipe.path;
60 } else if (index.column() == PipesDelegate::DirectionColumn) {
61 return pipe.direction;
62 } else if (index.column() == PipesDelegate::ContentsColumn) {
63 return pipe.pipeContents;
64 }
65 }
66 }
67 return QVariant();
68 }
69
removeRow(int row,const QModelIndex &)70 bool PipesModel::removeRow(int row, const QModelIndex & /* parent */)
71 {
72 if (row < mPipesList.size() && row >= 0) {
73 mPipesList.removeAt(row);
74 reset();
75 return true;
76 }
77 return false;
78 }
79
headerData(int section,Qt::Orientation orientation,int role) const80 QVariant PipesModel::headerData(int section, Qt::Orientation orientation, int role) const
81 {
82 if (orientation == Qt::Horizontal) {
83 if (role == Qt::DisplayRole) {
84 if (section == PipesDelegate::EnabledColumn) {
85 return QVariant();
86 } else if (section == PipesDelegate::PathColumn) {
87 return i18n("Path");
88 } else if (section == PipesDelegate::DirectionColumn) {
89 return i18n("Direction");
90 } else if (section == PipesDelegate::ContentsColumn) {
91 return i18n("Pipe Contents");
92 }
93 } else if (role == Qt::TextAlignmentRole) {
94 return Qt::AlignCenter;
95 }
96 }
97 return QAbstractItemModel::headerData(section, orientation, role);
98 }
99
setData(const QModelIndex & index,const QVariant & value,int role)100 bool PipesModel::setData(const QModelIndex &index, const QVariant &value, int role)
101 {
102 if (index.isValid()) {
103 // basically, the row index is the same as the index in mPipesList, so we can
104 // associate these and work from there
105 if (role == Qt::EditRole) {
106 if (index.column() == PipesDelegate::PathColumn) {
107 mPipesList[index.row()].path = value.toString();
108 } else if (index.column() == PipesDelegate::DirectionColumn) {
109 mPipesList[index.row()].direction = (PipesPlugin::PipeDirection)value.toInt();
110 } else if (index.column() == PipesDelegate::ContentsColumn) {
111 mPipesList[index.row()].pipeContents = (PipesPlugin::PipeContents)value.toInt();
112 } else {
113 return false;
114 }
115
116 emit dataChanged(index, index);
117 return true;
118 } else if (role == Qt::CheckStateRole) {
119 if (index.column() == PipesDelegate::EnabledColumn) {
120 mPipesList[index.row()].enabled = value.toBool();
121 emit dataChanged(index, index);
122 return true;
123 }
124 }
125 }
126 return false;
127 }
128
flags(const QModelIndex & index) const129 Qt::ItemFlags PipesModel::flags(const QModelIndex &index) const
130 {
131 Qt::ItemFlags flags = QAbstractItemModel::flags(index);
132 // enabled, direction, and contents is editable, while path is not
133 if (index.column() == PipesDelegate::EnabledColumn || index.column() == PipesDelegate::DirectionColumn || index.column() == PipesDelegate::ContentsColumn) {
134 flags |= Qt::ItemIsEditable;
135 }
136 return flags;
137 }
138
setPipes(PipesPlugin::PipeOptionsList pipes)139 void PipesModel::setPipes(PipesPlugin::PipeOptionsList pipes)
140 {
141 mPipesList = pipes;
142 reset();
143 }
144
pipes()145 PipesPlugin::PipeOptionsList PipesModel::pipes()
146 {
147 return mPipesList;
148 }
149
addPipe(const PipesPlugin::PipeOptions & pipe)150 void PipesModel::addPipe(const PipesPlugin::PipeOptions &pipe)
151 {
152 mPipesList.append(pipe);
153 reset();
154 }
155