1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include "DocListPanel.h"
#include <QTreeView>
#include <QLineEdit>
#include <QVBoxLayout>
#include "JuffAPI.h"
FileListModel::FileListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int FileListModel::rowCount(const QModelIndex & parent) const
{
Q_UNUSED(parent);
return m_files.count();
}
int FileListModel::columnCount(const QModelIndex & parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant FileListModel::data(const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
if (index.column() == 0)
return m_files.values().at(index.row());
else
return m_files.keys().at(index.row());
break;
case Qt::DecorationRole:
if (index.column() == 0)
return m_icons.icon(QFileInfo(m_files.keys().at(index.row())));
break;
case Qt::BackgroundRole:
if (m_files.keys().at(index.row()) == m_currentFile)
return QPalette().highlight();
break;
case Qt::ToolTipRole:
// upside-down of DisplayRole
if (index.column() == 0)
return m_files.keys().at(index.row());
else
return m_files.values().at(index.row());
break;
}
return QVariant();
}
QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Vertical)
return QVariant();
// QIcon(QString(":doc_icon"))
// tooltip = name
return section == 0 ? tr("Name") : tr("Full name");
}
QString FileListModel::file(const QModelIndex &index)
{
if (!index.isValid())
return QString();
return m_files.keys().at(index.row());
}
void FileListModel::docOpened(const QString &fname, const QString &title)
{
beginResetModel();
m_files[fname] = title;
endResetModel();
}
void FileListModel::docActivated(const QString &fname)
{
beginResetModel();
m_currentFile = fname;
endResetModel();
}
void FileListModel::docClosed(const QString &fname)
{
beginResetModel();
m_files.remove(fname);
endResetModel();
}
void FileListModel::docRenamed(const QString &fname, const QString &title, const QString &oldName)
{
Q_UNUSED(oldName);
beginResetModel();
m_files[fname] = title;
endResetModel();
}
void FileListModel::docModified(const QString &fname, const QString &title)
{
beginResetModel();
m_files[fname] = title;
endResetModel();
}
FileListFilterModel::FileListFilterModel(QWidget *parent)
: QSortFilterProxyModel(parent)
{
}
DocListPanel::DocListPanel(JuffAPI *api)
: QWidget(0),
api_(api)
{
setWindowTitle(tr("Documents"));
tree_ = new QTreeView(this);
tree_->setAlternatingRowColors(true);
model_ = new FileListModel(this);
proxy_ = new FileListFilterModel(this);
proxy_->setSourceModel(model_);
tree_->setModel(proxy_);
proxy_->setFilterKeyColumn(0);
#if QT_VERSION < 0x050200
filter_ = new FilterLineEdit();
filter_->setMaximumHeight(24);
#else
filter_ = new QLineEdit;
filter_->setPlaceholderText(tr("Filter"));
filter_->setClearButtonEnabled(true);
#endif
QVBoxLayout* vBox = new QVBoxLayout();
vBox->setMargin(0);
vBox->setSpacing(2);
setLayout(vBox);
vBox->addWidget(tree_);
vBox->addWidget(filter_);
#if QT_VERSION < 0x050200
connect(filter_->lineEd_, SIGNAL(textChanged(const QString&)),
proxy_, SLOT(setFilterFixedString(const QString&)));
connect(filter_->clearBtn_, SIGNAL(clicked()), SLOT(clear()));
#else
connect(filter_, SIGNAL(textChanged(const QString&)),
proxy_, SLOT(setFilterFixedString(const QString&)));
#endif
connect(tree_, SIGNAL(activated(QModelIndex)),
this, SLOT(docClicked(QModelIndex)));
}
void DocListPanel::docClicked(const QModelIndex &index) {
// LOGGER;
if (!index.isValid())
return;
api_->openDoc(model_->file(proxy_->mapToSource(index)));
}
void DocListPanel::docOpened(const QString &fname, const QString &title)
{
model_->docOpened(fname, title);
tree_->resizeColumnToContents(0);
}
void DocListPanel::docActivated(const QString &fname)
{
model_->docActivated(fname);
tree_->resizeColumnToContents(0);
}
void DocListPanel::docClosed(const QString &fname)
{
model_->docClosed(fname);
tree_->resizeColumnToContents(0);
}
void DocListPanel::docRenamed(const QString &fname, const QString &title, const QString &oldName)
{
model_->docRenamed(fname, title, oldName);
tree_->resizeColumnToContents(0);
}
void DocListPanel::docModified(const QString &fname, const QString &title)
{
model_->docModified(fname, title);
tree_->resizeColumnToContents(0);
}
#if QT_VERSION < 0x050200
void DocListPanel::clear() {
#if 0
filter_->lineEd_->setText("");
#endif
}
#endif
|