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
|
#include "fileselector.h"
FileSelector::FileSelector(QWidget *parent, bool multiselect) :
QWidget(parent), multiselect(multiselect)
{
setLayout(new QVBoxLayout());
list = new QListWidget(this);
layout()->addWidget(list);
filter = new QLineEdit(this);
layout()->addWidget(filter);
connect(filter, SIGNAL(textChanged(QString)), SLOT(filterChanged(QString)));
filter->installEventFilter(this);
list->installEventFilter(this);
QPalette p = QApplication::palette(); //let the list appear selected (does not work with gtk+ style)
p.setColor(QPalette::Inactive, QPalette::Highlight, p.color(QPalette::Active, QPalette::Highlight));
p.setColor(QPalette::Inactive, QPalette::HighlightedText, p.color(QPalette::Active, QPalette::HighlightedText));
list->setPalette(p);
setAttribute(Qt::WA_DeleteOnClose);
if (multiselect)
list->setSelectionMode(QAbstractItemView::ExtendedSelection);
}
void FileSelector::init(const QStringList &files, int current)
{
filter->setFocus();
rawFiles = files;
filterChanged(filter->text());
if (current >= 0 && current < rawFiles.count()) {
int r = 0;
for (int i = 0; i < current; i++)
if (list->item(i)->text() == rawFiles[i]) r++;
if (list->item(r)->text() == rawFiles[current])
list->setCurrentRow(r);
}
}
void FileSelector::setCentered(const QRect &rect)
{
QSize s = rect.size();
QPoint p = rect.topLeft();
int fsw = s.width() / 2;
int scrollbarwidth = 50; //value that works on my computer...
/*if (list->verticalScrollBar()) {
QStyleOptionSlider sos;
sos.initFrom(list->verticalScrollBar());
scrollbarwidth = qAbs(list->verticalScrollBar()->style()->subControlRect(QStyle::CC_ScrollBar, &sos, QStyle::SC_ScrollBarGroove, list->verticalScrollBar()).width());
}*/
QFontMetrics fm = list->fontMetrics();
for (int i = 0; i < rawFiles.size(); i++)
fsw = qMax(fsw, fm.width(rawFiles[i]) + scrollbarwidth );
fsw = qMin(fsw, s.width());
setGeometry(s.width() / 2 - fsw / 2 + p.x(), s.height() / 4 + p.y(), fsw, s.height() / 2);
setMinimumWidth(fsw); //set geometry alone leads to a too small window. but we need to call setGeometry first, or it crashes if fsw = s.width()
}
void FileSelector::filterChanged(const QString &newFilter)
{
QString nf = newFilter;
if (newFilter.contains(':') && QRegExp(".*:[0-9; ]*").exactMatch(newFilter))
nf = newFilter.left(newFilter.lastIndexOf(':'));
QStringList filterList = nf.split(" ");
QList<QPair<QString, int> > oldFiles = currentFiles();
list->clear();
foreach (const QString &s, rawFiles) {
bool skip = false;
foreach (const QString &tf, filterList)
if (!s.contains(tf, Qt::CaseInsensitive)) {
skip = true;
break;
}
if (skip) continue;
list->addItem(s);
}
if (!oldFiles.isEmpty()) {
bool foundOne = false;
for (int o = 0; o < oldFiles.size(); o++) {
const QPair<QString, int> &oldFile = oldFiles[o];
int duplicate = oldFile.second;
for (int i = 0; i < list->count(); i++)
if (list->item(i)->text() == oldFile.first) {
duplicate -= 1;
if (duplicate < 0) {
if (!foundOne) list->setCurrentRow(i);
else if (list->selectionModel() && list->model()) list->selectionModel()->select(list->model()->index(i, 0), QItemSelectionModel::Select);
foundOne = true;
break;
}
}
}
if (foundOne)
return;
}
list->setCurrentRow(0);
}
bool FileSelector::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
int key = static_cast<QKeyEvent *>(event)->key();
switch (key) {
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
if (obj != filter) break;
{
int offset = -1;
if (key == Qt::Key_Down) offset = 1;
else if (key == Qt::Key_PageUp) offset = - list->height() / qMax(1, list->fontMetrics().height());
else if (key == Qt::Key_PageDown) offset = list->height() / qMax(1, list->fontMetrics().height());
int row = list->currentRow() + offset;
if (row + offset < 0) row = 0;
else if (row >= list->count()) row = list->count() - 1;
if (row == list->currentRow()) break;
list->setCurrentRow(row);
return true;
}
case Qt::Key_Return:
case Qt::Key_Enter:
emitChoosen();
return true;
case Qt::Key_Escape:
close();
return true;
}
} else if (obj == list && event->type() == QEvent::MouseButtonDblClick) {
qDebug() << "??? todo, why is this not called ??";
emitChoosen();
return true;
}
return QObject::eventFilter(obj, event);
}
typedef QPair<QString, int> QPairStringInt;
void FileSelector::emitChoosen()
{
QString jumpTo = filter->text().mid(filter->text().lastIndexOf(':') + 1);
int line = -1, col = -1;
if (!jumpTo.isEmpty()) {
if (!jumpTo.contains(';')) line = jumpTo.trimmed().toInt() - 1;
else {
QStringList sl = jumpTo.split(';');
line = sl[0].trimmed().toInt() - 1;
col = sl[1].trimmed().toInt();
}
}
foreach (const QPairStringInt &p, currentFiles())
emit fileChoosen(p.first, p.second, line, col);
close();
}
QList<QPair<QString, int> > FileSelector::currentFiles()
{
QList<QPair<QString, int> > result;
int cindex = list->currentRow();
if (cindex < 0 || cindex >= rawFiles.count()) return result;
QList<int> indices;
if (!multiselect) indices << cindex;
else if (list->selectionModel())
foreach (const QModelIndex &index, list->selectionModel()->selectedIndexes())
indices << index.row();
foreach (int index, indices) {
QString file = list->item(index)->text();
int duplicate = 0;
for (int i = 0; i < index; i++)
if (list->item(i)->text() == file)
duplicate++;
result << QPair<QString, int>(file, duplicate);
}
return result;
}
|