Skip to content

Commit

Permalink
fix: improve folder list index handling
Browse files Browse the repository at this point in the history
- Store folder index in item's UserRole data instead of relying on row number
- Fix potential mismatch between visible items and data source
- Optimize variable initialization order

Log: improve folder list index handling
Bug: https://pms.uniontech.com/bug-view-289995.html
  • Loading branch information
wyu71 committed Dec 6, 2024
1 parent 86d10f8 commit a5df475
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void FolderListWidgetPrivate::clicked(const QModelIndex &index)
q->hide();

if (index.isValid()) {
int row = index.row();
int row = index.data(Qt::UserRole).toInt();
if (row >= 0 && row < crumbDatas.size()) {
Q_EMIT q->urlButtonActivated(crumbDatas[row].url);
}
Expand Down Expand Up @@ -136,24 +136,25 @@ FolderListWidget::~FolderListWidget() = default;
void FolderListWidget::setFolderList(const QList<CrumbData> &datas, bool stacked)
{
d->folderModel->clear();
d->crumbDatas = datas;
int dataNum = 0;
d->crumbDatas = datas;
bool isShowedHiddenFiles = true;
if (!stacked)
isShowedHiddenFiles = Application::instance()->genericAttribute(Application::kShowedHiddenFiles).toBool();

// Calculate max text width
int maxTextWidth = 0;
QFontMetrics fm(font());
for (auto data : datas) {
auto info = InfoFactory::create<FileInfo>(data.url);
for (int i = 0; i < datas.size(); ++i) {
auto info = InfoFactory::create<FileInfo>(datas[i].url);
if (!info.isNull() && (isShowedHiddenFiles || !info->isAttributes(FileInfo::FileIsType::kIsHidden))) {
QStandardItem *item = new QStandardItem(info->fileIcon(), data.displayText);
QStandardItem *item = new QStandardItem(info->fileIcon(), datas[i].displayText);
item->setData(i, Qt::UserRole);
d->folderModel->insertRow(dataNum, item);
dataNum++;

// Calculate text width
int textWidth = fm.horizontalAdvance(data.displayText);
int textWidth = fm.horizontalAdvance(datas[i].displayText);
maxTextWidth = qMax(maxTextWidth, textWidth);
}
}
Expand Down

0 comments on commit a5df475

Please sign in to comment.