Skip to content
Permalink
Browse files

fixed FolderView cursor bug.(refs #76)

  • Loading branch information...
haraki committed May 31, 2019
1 parent 24aa7ac commit a6b7695aa446a0123cb88ff15dece893e6742b79
Showing with 82 additions and 28 deletions.
  1. +35 −25 folderform.cpp
  2. +3 −1 folderform.h
  3. +38 −2 foldermodel.cpp
  4. +6 −0 foldermodel.h
@@ -21,7 +21,6 @@ FolderForm::FolderForm(FilterFlags filterFlags,
: QWidget(parent)
, ui(new Ui::FolderForm)
, m_folderModel(new FolderModel(this))
, m_isSettingPath(false)
{
ui->setupUi(this);

@@ -45,6 +44,14 @@ FolderForm::FolderForm(FilterFlags filterFlags,
SIGNAL(directoryLoaded(const QString&)),
this,
SLOT(onDirectoryLoaded(const QString&)));
connect(m_folderModel,
SIGNAL(layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)),
this,
SLOT(onLayoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)));
connect(m_folderModel,
SIGNAL(layoutAboutToBeChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)),
this,
SLOT(onLayoutAboutToBeChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)));

ui->folderView->installEventFilter(this);
}
@@ -180,14 +187,6 @@ Qt::SortOrder FolderForm::getSortOrder() const

int FolderForm::setPath(const QString& dirPath)
{
if(m_isSettingPath)
{
// 前回の setPath() が終わっていない
qDebug() << "previous setPath() has not ended.";

return -1;
}

// ディレクトリが空(".." も存在しない)場合は Open できないとみなしてエラー
if(QDir(dirPath).isEmpty(QDir::AllEntries | QDir::NoDot))
{
@@ -196,8 +195,6 @@ int FolderForm::setPath(const QString& dirPath)
return -1;
}

m_isSettingPath = true;

m_folderModel->clearSelected();

m_folderModel->setRootPath(dirPath);
@@ -267,27 +264,39 @@ void FolderForm::onDirectoryLoaded(const QString& path)
{
qDebug() << "directory loaded." << path;

if(m_isSettingPath)
QModelIndex currentRootIndex = ui->folderView->rootIndex();
QModelIndex newRootIndex = m_folderModel->index(path);

ui->folderView->setRootIndex(newRootIndex);

if(currentRootIndex.parent() == newRootIndex)
{
// 前回のパスが子ディレクトリであれば、そこを初期カーソル位置とする
QModelIndex newCursorIndex = ui->folderView->rootIndex();
ui->folderView->setCursor(currentRootIndex);
}
}

// setPath() によって発生した場合はカーソル位置を再設定する
QModelIndex newDirIndex = m_folderModel->index(path);
ui->folderView->setRootIndex(newDirIndex);
void FolderForm::onLayoutChanged(const QList<QPersistentModelIndex> &parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
qDebug() << "FolderForm::onLayoutChanged() parents : " << parents << ", hint : " << hint;

if(!newCursorIndex.isValid() || newCursorIndex.parent() != newDirIndex || newCursorIndex.row() < 0)
{
// 初期カーソル位置はリストの先頭
newCursorIndex = m_folderModel->index(0, 0, newDirIndex);
}
QModelIndex cursorIndex = ui->folderView->currentIndex();

ui->folderView->setCursor(newCursorIndex);
qDebug() << "============================= cursorIndex.isValid() : " << cursorIndex.isValid() << ", cursorIndex.row() : " << cursorIndex.row();

m_isSettingPath = false;
if(!cursorIndex.isValid() || cursorIndex.row() < 0)
{
// 初期カーソル位置はリストの先頭
cursorIndex = m_folderModel->index(0, 0, ui->folderView->rootIndex());
ui->folderView->setCursor(cursorIndex);
}
}

void FolderForm::onLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
qDebug() << "FolderForm::onLayoutAboutToBeChanged()";

}

void FolderForm::setCursor(const QString& fileName)
{
const QString currentPath = getCurrentDirPath();
@@ -332,14 +341,15 @@ int FolderForm::onGoToParentDir()
void FolderForm::refresh()
{
m_folderModel->refresh();

#if 0
QModelIndex cursorIndex = ui->folderView->currentIndex();
if(!cursorIndex.isValid() || cursorIndex.parent() != ui->folderView->rootIndex() || cursorIndex.row() < 0)
{
cursorIndex = m_folderModel->index(0, 0, ui->folderView->rootIndex());
}

ui->folderView->setCursor(cursorIndex);
#endif
}

int FolderForm::getTotalColumnWidth(int withOutColumn)
@@ -75,6 +75,8 @@ class FolderForm : public QWidget
protected Q_SLOTS:
void onCurrentChanged(const QModelIndex& newIndex, const QModelIndex& oldIndex);
void onDirectoryLoaded(const QString &path);
void onLayoutChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
void onLayoutAboutToBeChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);

private Q_SLOTS:
void on_folderSelectButton_clicked();
@@ -92,7 +94,7 @@ private Q_SLOTS:

FolderModel *m_folderModel;

bool m_isSettingPath;
// bool m_isSettingPath;
};

} // namespace Farman
@@ -41,6 +41,12 @@ FolderModel::FolderModel(QObject *parent/* = Q_NULLPTR*/) :
fsModel->setFilter(FIX_FILTER_FLAGS);
fsModel->setNameFilterDisables(false);

// layoutChanged シグナルを受け取るスロット QSortFilterProxyModel::_q_sourceLayoutChanged() 内において、フィルタ及びソートが実行される
// フィルタ及びソート実行後に FolderModel::onLayoutChanged() を呼ばれるようにするため、
// 上記のシグナルとスロットを connect している setSourceModel() を必ず先に呼び出しておく必要がある
// (同一シグナルに対するスロットの呼び出し順は、connect された順(Qt v5.12.3 で確認))
setSourceModel(fsModel);

connect(fsModel,
SIGNAL(rootPathChanged(QString)),
this,
@@ -53,8 +59,14 @@ FolderModel::FolderModel(QObject *parent/* = Q_NULLPTR*/) :
SIGNAL(directoryLoaded(QString)),
this,
SLOT(onDirectoryLoaded(QString)));

setSourceModel(fsModel);
connect(fsModel,
SIGNAL(layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)),
this,
SLOT(onLayoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)));
connect(fsModel,
SIGNAL(layoutAboutToBeChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)),
this,
SLOT(onLayoutAboutToBeChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint)));
}

FolderModel::~FolderModel()
@@ -77,6 +89,16 @@ void FolderModel::onDirectoryLoaded(const QString &path)
emitDirectoryLoaded(path);
}

void FolderModel::onLayoutChanged(const QList<QPersistentModelIndex> &parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
emitLayoutChanged(parents, hint);
}

void FolderModel::onLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
emitLayoutAboutToBeChanged(parents, hint);
}

QModelIndex FolderModel::index(const QString &path, int column/* = 0*/) const
{
QFileSystemModel* fsModel = qobject_cast<QFileSystemModel*>(sourceModel());
@@ -415,6 +437,8 @@ bool FolderModel::filterAcceptsRow(int source_row, const QModelIndex &source_par
// source_parent は mapToSource されているようなので、QFileSystemModel に渡す際は mapToSource しない
QFileInfo pfi = fsModel->fileInfo(source_parent);

// qDebug() << "FolderModel::filterAcceptsRow() : source_row : " << source_row << ", source_parent : " << pfi.filePath();

QModelIndex childIndex = fsModel->index(source_row, 0, source_parent);
QFileInfo cfi = fsModel->fileInfo(childIndex);

@@ -445,6 +469,8 @@ bool FolderModel::lessThan(const QModelIndex &source_left, const QModelIndex &so
QFileInfo r_info = fsModel->fileInfo(source_right);
bool ascOrder = (m_sortOrder == Qt::AscendingOrder);

// qDebug() << "FolderModel::lessThan() : source_left : " << l_info.filePath() << ", source_right : " << r_info.filePath();

if(m_sortDotFirst)
{
if(l_info.fileName() == ".")
@@ -748,6 +774,16 @@ void FolderModel::emitDirectoryLoaded(const QString &path)
emit directoryLoaded(path);
}

void FolderModel::emitLayoutChanged(const QList<QPersistentModelIndex>& parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
emit layoutChanged(parents, hint);
}

void FolderModel::emitLayoutAboutToBeChanged(const QList<QPersistentModelIndex>& parents/* = QList<QPersistentModelIndex>()*/, QAbstractItemModel::LayoutChangeHint hint/* = QAbstractItemModel::NoLayoutChangeHint*/)
{
emit layoutAboutToBeChanged(parents, hint);
}

// QFileSystemModel specific API
QModelIndex FolderModel::setRootPath(const QString &path)
{
@@ -102,11 +102,15 @@ class FolderModel : public QSortFilterProxyModel
void rootPathChanged(const QString &newPath);
void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
void directoryLoaded(const QString &path);
void layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);

private Q_SLOTS:
void onRootPathChanged(const QString &newPath);
void onFileRenamed(const QString &path, const QString &oldName, const QString &newName);
void onDirectoryLoaded(const QString &path);
void onLayoutChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
void onLayoutAboutToBeChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);

private:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const Q_DECL_OVERRIDE;
@@ -124,6 +128,8 @@ private Q_SLOTS:
void emitRootPathChanged(const QString &newPath);
void emitFileRenamed(const QString &path, const QString &oldName, const QString &newName);
void emitDirectoryLoaded(const QString &path);
void emitLayoutChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
void emitLayoutAboutToBeChanged(const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);

FilterFlags m_filterFlags;

0 comments on commit a6b7695

Please sign in to comment.
You can’t perform that action at this time.