赞
踩
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QFileDialog> #include <QTreeWidgetItem> #include <QTreeWidget> #include <QPixmap> #include <QLabel> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT private: enum treeItemType {itTopItem=1001,itGroupItem,itImageItem}; enum treeColNum {colItem=0,colItemType=1}; QPixmap pixmap; QLabel labFileName; float pixRatio; void changeItemCaption(QTreeWidgetItem *item); public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_actOpenFolder_triggered(); void on_actOpenFile_triggered(); void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); void on_actWidth_triggered(); void on_actIter_triggered(); void on_actDel_triggered(); void on_actHeight_triggered(); void on_actZoomIn_triggered(); void on_actZoomOut_triggered(); void on_actRealSize_triggered(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->statusbar->addWidget(&labFileName); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_actOpenFolder_triggered() { QString fullname = QFileDialog::getExistingDirectory(this,"open Folder","."); if(fullname.isEmpty()) return; QString dir = fullname.right(fullname.length()-fullname.lastIndexOf('/')-1); if(!ui->treeWidget->currentIndex().isValid()) return; QTreeWidgetItem* parItem = ui->treeWidget->currentItem(); QTreeWidgetItem* item = new QTreeWidgetItem(itGroupItem); QIcon icon(":/images/icon/open.ico"); item->setIcon(treeColNum::colItem, icon); item->setText(treeColNum::colItem, dir); item->setText(treeColNum::colItemType, "type:group"); item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable); item->setCheckState(colItem,Qt::Checked); parItem->addChild(item); } void MainWindow::on_actOpenFile_triggered() { QStringList files = QFileDialog::getOpenFileNames(this,"file or files",".","Images(*.jpg *.png)"); if (files.isEmpty()) return; QTreeWidgetItem* parItem; QTreeWidgetItem* item = ui->treeWidget->currentItem(); if(item->type() == treeItemType::itImageItem) parItem = item->parent(); else parItem = item; foreach(auto &file, files){ item = new QTreeWidgetItem(treeItemType::itImageItem); QString name = file.right(file.length()-file.lastIndexOf('/')-1); QIcon icon(":/images/icon/picture.ico"); item->setIcon(treeColNum::colItem, icon); item->setText(treeColNum::colItem, name); item->setText(treeColNum::colItemType, "type:Image"); item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable); item->setCheckState(colItem,Qt::Checked); item->setData(treeColNum::colItem,Qt::UserRole,file); parItem->addChild(item); } } void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { Q_UNUSED(previous); if(current == NULL) return; int var = current->type(); switch (var) { case treeItemType::itTopItem: ui->actOpenFile->setEnabled(true); ui->actOpenFolder->setEnabled(true); ui->actDel->setEnabled(false); break; case treeItemType::itGroupItem: ui->actOpenFile->setEnabled(true); ui->actOpenFolder->setEnabled(true); ui->actDel->setEnabled(true); break; case treeItemType::itImageItem: ui->actOpenFile->setEnabled(true); ui->actOpenFolder->setEnabled(false); ui->actDel->setEnabled(true); labFileName.setText(current->data(colItem,Qt::UserRole).toString()); pixmap.load(current->data(colItem,Qt::UserRole).toString()); on_actWidth_triggered(); ui->actWidth->setEnabled(true); ui->actHeight->setEnabled(true); ui->actZoomIn->setEnabled(true); ui->actZoomOut->setEnabled(true); ui->actRealSize->setEnabled(true); break; } } void MainWindow::on_actWidth_triggered() { int W = ui->scrollArea->width(); int realW = pixmap.width(); pixRatio = (float)W/realW; QPixmap pix = pixmap.scaledToWidth(W-30); ui->label->setPixmap(pix); } void MainWindow::on_actIter_triggered() { for (int i=0; i<ui->treeWidget->topLevelItemCount(); i++) { QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i); changeItemCaption(item); } } void MainWindow::changeItemCaption(QTreeWidgetItem *item) { QString str = "x_"+item->text(colItem); item->setText(colItem,str); for (int i=0; i<item->childCount(); i++) { changeItemCaption(item->child(i)); } } void MainWindow::on_actDel_triggered() { QTreeWidgetItem* item = ui->treeWidget->currentItem(); if (!item->parent()) { ui->actDel->setEnabled(false); return; } QTreeWidgetItem* ParItem = item->parent(); ParItem->removeChild(item); delete item; } void MainWindow::on_actHeight_triggered() { int H = ui->scrollArea->height(); int realH = pixmap.height(); pixRatio = (float)H/realH; QPixmap pix = pixmap.scaledToHeight(H-30); ui->label->setPixmap(pix); } void MainWindow::on_actZoomIn_triggered() { pixRatio *= 1.2; int w = pixRatio*pixmap.width(); int h = pixRatio*pixmap.height(); QPixmap pix = pixmap.scaled(w,h); ui->label->setPixmap(pix); } void MainWindow::on_actZoomOut_triggered() { pixRatio *= 0.8; int w = pixRatio*pixmap.width(); int h = pixRatio*pixmap.height(); QPixmap pix = pixmap.scaled(w,h); ui->label->setPixmap(pix); } void MainWindow::on_actRealSize_triggered() { ui->label->setPixmap(pixmap); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。