Skip to content

Commit

Permalink
allow hiding of images in table view (giowck#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshirio committed Jan 5, 2020
1 parent 061f0e6 commit 80756e4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 43 deletions.
10 changes: 10 additions & 0 deletions ui/preferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,16 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="hideImagesTableViewCheckBox">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Images that are loaded from disk in table view can cause a poor scrolling experience. &lt;br/&gt;Hiding images from the table view allows a smooth scrolling experience on weak devices.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Improve scrolling in table view by hiding images</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
97 changes: 54 additions & 43 deletions views/tableview/tableviewdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ TableViewDelegate::TableViewDelegate(QObject *parent) :
if (m_cacheImages) {
QPixmapCache::setCacheLimit(10240 * 200); //200 * 10MB ~ 2GB RAM needed
}

//load property: images should be hidden for better scrolling performance on weak devices
m_hideImages = sm.restoreProperty("hideImages", "tableView").toBool();
}

void TableViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -796,54 +799,62 @@ void TableViewDelegate::paintImageType(QPainter *painter,
int fileId = index.data().toInt();
if (!fileId) return;

QStyleOptionViewItem opt(option);

QString filePath;
QString fileName;
QString fileHash;
QString origDirPath;
QDateTime addedDateTime;
FileManager fm;

m_metadataEngine->getContentFile(fileId,
fileName,
fileHash,
addedDateTime,
origDirPath);

//if file was not found
if (fileHash.isEmpty()) return;

filePath = fm.getFilesDirectory() + fileHash;
QPixmap pixmap;

//use caching if enabled
if (m_cacheImages) {
if (!QPixmapCache::find(fileHash, &pixmap)) {
//check if images should be hidden for better scrolling performance
if (m_hideImages) { //show hidden as standard text
QStyleOptionViewItem opt(option);
opt.text = tr("hidden");
opt.font.setStyle(QFont::StyleItalic);
opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
} else { // show image
QStyleOptionViewItem opt(option);

QString filePath;
QString fileName;
QString fileHash;
QString origDirPath;
QDateTime addedDateTime;
FileManager fm;

m_metadataEngine->getContentFile(fileId,
fileName,
fileHash,
addedDateTime,
origDirPath);

//if file was not found
if (fileHash.isEmpty()) return;

filePath = fm.getFilesDirectory() + fileHash;
QPixmap pixmap;

//use caching if enabled
if (m_cacheImages) {
if (!QPixmapCache::find(fileHash, &pixmap)) {
pixmap.load(filePath);
pixmap = pixmap.scaled(QSize(opt.rect.width(),
opt.rect.height()),
Qt::KeepAspectRatio,
Qt::FastTransformation);
QPixmapCache::insert(fileHash, pixmap);
}
} else {
pixmap.load(filePath);
pixmap = pixmap.scaled(QSize(opt.rect.width(),
opt.rect.height()),
Qt::KeepAspectRatio,
Qt::FastTransformation);
QPixmapCache::insert(fileHash, pixmap);
}
} else {
pixmap.load(filePath);
}

QRect imageRect = pixmap.scaled(QSize(opt.rect.width(),
opt.rect.height()),
Qt::KeepAspectRatio,
Qt::FastTransformation).rect();
QRect drawRect(opt.rect);
QRect imageRect = pixmap.scaled(QSize(opt.rect.width(),
opt.rect.height()),
Qt::KeepAspectRatio,
Qt::FastTransformation).rect();
QRect drawRect(opt.rect);

//center
drawRect.moveLeft(opt.rect.center().x() - (imageRect.width() / 2));
drawRect.moveTop(opt.rect.center().y() - (imageRect.height() / 2));
//center
drawRect.moveLeft(opt.rect.center().x() - (imageRect.width() / 2));
drawRect.moveTop(opt.rect.center().y() - (imageRect.height() / 2));

drawRect.setWidth(imageRect.width());
drawRect.setHeight(imageRect.height());
painter->drawPixmap(drawRect, pixmap);
drawRect.setWidth(imageRect.width());
drawRect.setHeight(imageRect.height());
painter->drawPixmap(drawRect, pixmap);
}
}

void TableViewDelegate::paintFilesType(QPainter *painter,
Expand Down
1 change: 1 addition & 0 deletions views/tableview/tableviewdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private slots:

MetadataEngine *m_metadataEngine;
bool m_cacheImages;
bool m_hideImages;
};

#endif // TABLEVIEWDELEGATE_H
18 changes: 18 additions & 0 deletions widgets/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) :
this, SLOT(tableViewRowSizeSpinChanged()));
connect(ui->cacheImagesTableViewCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(cacheImagesTableViewCheckBoxChanged()));
connect(ui->hideImagesTableViewCheckBox, &QCheckBox::stateChanged,
this, &PreferencesDialog::hideImagesTableViewCheckBoxChanged);
connect(ui->browseDbPathButton, SIGNAL(clicked(bool)),
this, SLOT(browseDbPathButtonClicked()));
connect(ui->resetDbPathButton, SIGNAL(clicked(bool)),
Expand Down Expand Up @@ -245,6 +247,17 @@ void PreferencesDialog::cacheImagesTableViewCheckBoxChanged()
QMessageBox::Ok);
}

void PreferencesDialog::hideImagesTableViewCheckBoxChanged()
{
bool b = ui->hideImagesTableViewCheckBox->isChecked();

m_settingsManager->saveProperty("hideImages", "tableView", b);

QMessageBox::information(this, tr("Restart required!"),
tr("A restart is required for this setting to take effect."),
QMessageBox::Ok);
}

void PreferencesDialog::browseDbPathButtonClicked()
{
QString currentDbPath;
Expand Down Expand Up @@ -358,6 +371,11 @@ void PreferencesDialog::loadSettings()
bool cacheImg = m_settingsManager->restoreProperty(
"cacheImages", "tableView").toBool();
ui->cacheImagesTableViewCheckBox->setChecked(cacheImg);

//table view img hiding
bool hideImg = m_settingsManager->restoreProperty(
"hideImages", "tableView").toBool();
ui->hideImagesTableViewCheckBox->setChecked(hideImg);
}

void PreferencesDialog::updateDatabasePath()
Expand Down
1 change: 1 addition & 0 deletions widgets/preferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private slots:
void formViewFontComboChanged();
void tableViewRowSizeSpinChanged();
void cacheImagesTableViewCheckBoxChanged();
void hideImagesTableViewCheckBoxChanged();
void browseDbPathButtonClicked();
void resetDbPathButtonClicked();

Expand Down

0 comments on commit 80756e4

Please sign in to comment.