std::unique_ptr<Image> fitImageTo(const std::unique_ptr<Image>& image, int w, int h) { int tw = image->getWidth(); int th = image->getHeight(); int pw = (w * image->getHeight()) / h; int ph = (h * image->getWidth()) / w; if (ph <= th) { auto middle_img = cropImage(image, 0, th/2 - ph/2, tw, ph); return shrinkImage(middle_img, w); } auto middle_img = cropImage(image, tw/2 - pw/2, 0, pw, th); return shrinkImage(middle_img, w); }
void ThumbnailPicker::slotSetFromList( int i ) { //Display image in preview pane QPixmap pm; pm = shrinkImage( PixList[i], 200, true ); //scale image SelectedImageIndex = i; ui->CurrentImage->setPixmap( pm ); ui->CurrentImage->update(); ui->EditButton->setEnabled( true ); *Image = PixList[i]->scaled( wid, ht, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); bImageFound = true; }
void ThumbnailPicker::slotSetFromURL() { //Attempt to load the specified URL QUrl u = ui->ImageURLBox->url(); if ( u.isValid() ) { if ( u.isLocalFile() ) { QFile localFile( u.toLocalFile() ); //Add image to list //If image is taller than desktop, rescale it. QImage im( localFile.fileName() ); if ( im.isNull() ) { KMessageBox::sorry( 0, i18n("Failed to load image at %1", localFile.fileName() ), i18n("Failed to load image") ); return; } uint w = im.width(); uint h = im.height(); uint pad = 0;/* FIXME later 4*marginHint() + 2*ui->SearchLabel->height() + actionButton( Ok )->height() + 25; */ uint hDesk = QApplication::desktop()->availableGeometry().height() - pad; if ( h > hDesk ) im = im.scaled( w*hDesk/h, hDesk, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); //Add Image to top of list and 50x50 thumbnail image and URL to top of listbox PixList.insert( 0, new QPixmap( QPixmap::fromImage( im ) ) ); ui->ImageList->insertItem( 0, new QListWidgetItem ( QIcon(shrinkImage( PixList.last(), 50 )), u.url() )); //Select the new image ui->ImageList->setCurrentRow( 0 ); slotSetFromList(0); } else { KIO::StoredTransferJob *j = KIO::storedGet( u, KIO::NoReload, KIO::HideProgressInfo ); j->setUiDelegate(0); connect( j, SIGNAL( result(KJob*) ), SLOT( slotJobResult(KJob*) ) ); } } }
void ThumbnailPicker::slotJobResult( KJob *job ) { KIO::StoredTransferJob *stjob = (KIO::StoredTransferJob*)job; //Update Progressbar if ( ! ui->SearchProgress->isHidden() ) { ui->SearchProgress->setValue(ui->SearchProgress->value()+1); if ( ui->SearchProgress->value() == ui->SearchProgress->maximum() ) { ui->SearchProgress->hide(); ui->SearchLabel->setText( i18n( "Search results:" ) ); } } //If there was a problem, just return silently without adding image to list. if ( job->error() ) { qDebug() << " error=" << job->error(); job->kill(); return; } QPixmap *pm = new QPixmap(); pm->loadFromData( stjob->data() ); uint w = pm->width(); uint h = pm->height(); uint pad = 0; /*FIXME LATER 4* QDialogBase::marginHint() + 2*ui->SearchLabel->height() + QDialogBase::actionButton( QDialogBase::Ok )->height() + 25;*/ uint hDesk = QApplication::desktop()->availableGeometry().height() - pad; if ( h > hDesk ) *pm = pm->scaled( w*hDesk/h, hDesk, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); PixList.append( pm ); //Add 50x50 image and URL to listbox //ui->ImageList->insertItem( shrinkImage( PixList.last(), 50 ), // cjob->srcURLs().first().prettyUrl() ); ui->ImageList->addItem( new QListWidgetItem ( QIcon(shrinkImage( PixList.last(), 50 )), stjob->url().url())); }