void ImageCanvasWidget::mouseReleaseEvent(QMouseEvent *event) { anchor_down_ = false; if (event->button() == Qt::RightButton) { // Get image from the canvas QImage selection = image_.copy(options_cache_->tile_selection()); emit SendImage(&selection); options_cache_->UpdateCursorShift(); } else if (event->button() == Qt::LeftButton) { // Set image back to the canvas UnsaveState(); emit RequestImage(); } }
ImageCache::ImageCache(const QString& cache_dir, size_t size) : network_manager_(this), cache_dir_(cache_dir), cache_(size), exit_(false), tick_(0), cache_thread_(new CacheThread(this)), network_request_semaphore_(MAXIMUM_NETWORK_REQUESTS) { QNetworkDiskCache* disk_cache = new QNetworkDiskCache(this); disk_cache->setCacheDirectory(cache_dir_); network_manager_.setCache(disk_cache); connect(&network_manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(ProcessReply(QNetworkReply*))); connect(cache_thread_, SIGNAL(RequestImage(QString)), this, SLOT(ProcessRequest(QString))); cache_thread_->start(); cache_thread_->setPriority(QThread::NormalPriority); }
void CacheThread::run() { while (!p->exit_) { // Wait until we're told there are images we need to request. waiting_mutex_.lock(); // Next, get all of them and sort them by priority. p->unprocessed_mutex_.lock(); QList<ImagePtr> images = p->unprocessed_.values(); p->unprocessed_mutex_.unlock(); qSort(images.begin(), images.end(), ComparePriority); // Go through all of them and request them. Qt's network manager will // only handle six simultaneous requests at once, so we use a semaphore // to limit ourselves to that many. // Each individual image will release the semaphore when it is done loading. // Also, only load up to a certain number at a time in this loop. If there // are more left afterward, we'll start over. This ensures that we // concentrate on processing the highest-priority images. int count = 0; while (!p->exit_ && !images.empty() && count < MAXIMUM_SEQUENTIAL_REQUESTS) { p->network_request_semaphore_.acquire(); ImagePtr image = images.front(); p->unprocessed_mutex_.lock(); if (!image->Loading() && !image->Failed()) { count++; image->SetLoading(true); images.pop_front(); QString uri = image->Uri(); size_t hash = p->uri_to_hash_map_[uri]; if (uri.startsWith(QString("file:///"))) { image->InitializeImage(); QString filepath = uri.replace(QString("file:///"), QString("/")); if (!image->GetImage()->load(filepath)) { image->ClearImage(); image->AddFailure(); } p->unprocessed_.remove(hash); p->uri_to_hash_map_.remove(uri); image->SetLoading(false); p->network_request_semaphore_.release(); } else { Q_EMIT RequestImage(image->Uri()); } } else { images.pop_front(); } p->unprocessed_mutex_.unlock(); } if (!images.empty()) { waiting_mutex_.unlock(); } } }