📄 pictureviewer.cpp
字号:
/************************************************************************************************************Copyright(C) 2008 Hailing Zhao.This widget may be used under the terms of the GNU General Public License versions 3.0 as published by the Free Software Foundation and appearing in the file LICENSE.GPL included in the packaging of this file. Please review the following information to ensure GNU General Public Licensing requirements will be met: <http://www.gnu.org/licenses/>.**************************************************************************************************************/#include "pictureviewer.h"#include <QDir>#include <QStringList>#include <QImage>#include <QPalette>#include <QModelIndex>#include <QToolTip>#include <QKeyEvent>#include <QCleanlooksStyle>PictureViewer::PictureViewer(QWidget *parent) : QWidget(parent){ setupUi(this); QApplication::setStyle(new QCleanlooksStyle); // initialize address comboBox comboBox->setEditable(true); comboBox->setInsertPolicy(QComboBox::InsertAtTop); comboBox->addItem(QIcon(":/folder.bmp"), "C:/Documents and Settings/ehailzh/My Documents/My Pictures"); comboBox->installEventFilter(this); comboBox->setAttribute(Qt::WA_Hover); // initialize listWidget listView->setViewMode(QListView::IconMode); listView->setIconSize(QSize(80,60)); listView->setResizeMode(QListView::Adjust); listModel = new PictureListModel(this); listView->setModel(listModel); listModel->addPictures("C:/Documents and Settings/ehailzh/My Documents/My Pictures"); connect(listView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(listItemChanged(QModelIndex, QModelIndex))); //initialize label label = new QLabel(this); label->setScaledContents(true); showPicture(listModel->index(0)); // Note: I have to use setWidget method to make scrollArea's scrollBars // visible according to label's size. If only drag label into scrollArea, // scrollBars won't appear. scrollArea->setBackgroundRole(QPalette::Dark); scrollArea->setWidgetResizable(false); scrollArea->setWidget(label); // initialize button toolButtonGo->setIcon(QIcon(":/go.bmp")); toolButtonPre->setIcon(QIcon(":/left.png")); toolButtonNext->setIcon(QIcon(":/right.png")); toolButtonGo->setToolTip(tr("Go to folder")); toolButtonPre->setToolTip(tr("Previous picture")); toolButtonNext->setToolTip(tr("Next picture")); toolButtonGo->setAttribute(Qt::WA_Hover); toolButtonPre->setAttribute(Qt::WA_Hover); toolButtonNext->setAttribute(Qt::WA_Hover);}PictureViewer::~PictureViewer(){}void PictureViewer::listItemChanged(const QModelIndex ¤t, const QModelIndex &previous){ showPicture(current);}void PictureViewer::on_toolButtonGo_clicked(){ QString dir = comboBox->currentText(); listModel->addPictures(dir); QModelIndex index = listModel->index(0); showPicture(index); if (index.isValid() && (comboBox->findText(dir, Qt::MatchFixedString) == -1)) { if (comboBox->count() > 5) { comboBox->removeItem(comboBox->count()-1); } comboBox->insertItem(0, QIcon(":/folder.bmp"), dir); }}void PictureViewer::on_toolButtonPre_clicked(){ QModelIndex current = listView->currentIndex(); QModelIndex preIndex = current.sibling(current.row()-1, current.column()); showPicture(preIndex);}void PictureViewer::on_toolButtonNext_clicked(){ QModelIndex current = listView->currentIndex(); QModelIndex nextIndex = current.sibling(current.row()+1, current.column()); showPicture(nextIndex);}void PictureViewer::showPicture(const QModelIndex &index){ if (!index.isValid()) return; QPixmap pixmap = listModel->data(index, Qt::UserRole).value<QPixmap>(); label->setPixmap(pixmap); label->adjustSize(); listView->setCurrentIndex(index);}bool PictureViewer::eventFilter(QObject *watched, QEvent *event){ if (watched == comboBox) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("pressed key = 0x%x\n", keyEvent->key()); if (keyEvent->key() == Qt::Key_Return) { on_toolButtonGo_clicked(); return true; } } } return QWidget::eventFilter(watched, event);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -