Example #1
0
// Invoke Function
bb::cascades::Image PictureHelper::getImage() {
	// Get random image from list and remove path
	if (m_imageCount == 0)
		initListImage();

	int rnd = rand() % m_imageCount;

	// Get imagedata and generate random width & height
	bb::ImageData img = fromQImage(loadQImage(listImg[rnd]));
	float ratio = (float)img.width() / (float)img.height();
	int max, min;	// max min to random width or height
	max = 700;
	min = 450;
	int rnd2 = (rand() % (max - min)) + min;
	if (ratio > 1) { //if width > height
		setImageWidth(rnd2);
		setImageHeight(rnd2 / ratio);
	} else {	// if width < height
		setImageHeight(rnd2);
		setImageWidth(rnd2 * ratio);
	}

	listImg.removeAt(rnd);
	setImageCount(listImg.count());

	return bb::cascades::Image(img);
}
Example #2
0
// Init list image to count for item
void PictureHelper::initListImage() {
	listImg.clear();
	QString appFolder(QDir::homePath());
	appFolder.chop(4);
	QDir imgPath(appFolder + "shared/camera");
	QDirIterator it(imgPath.absolutePath());
	while (it.hasNext()) {
		it.next();
		if ((it.filePath().contains(".png")) || (it.filePath().contains(".jpg"))) {
			listImg << it.filePath();
		}
	}
	setImageCount(listImg.count());
}
Example #3
0
void PageApi::setPageCount(int count, bool sure)
{
	if (m_pagesCount <= 0 || (!m_pagesCountSafe && sure))
	{
		m_pagesCount = count;
		m_pagesCountSafe = sure;

		if (sure)
		{
			int forcedLimit = m_api->forcedLimit();
			int perPage = forcedLimit > 0 ? forcedLimit : m_imagesPerPage;
			setImageCount(count * perPage, false);
		}
	}
}
Example #4
0
void PageApi::parseActual()
{
	// Try to read the reply
	m_source = m_reply->readAll();
	if (m_source.isEmpty() || m_reply->error() != QNetworkReply::NoError)
	{
		if (m_reply->error() != QNetworkReply::OperationCanceledError)
		{ log(QStringLiteral("[%1][%2] Loading error: %3 (%4)").arg(m_site->url(), m_format, m_reply->errorString()).arg(m_reply->error()), Logger::Error); }
		m_reply->deleteLater();
		m_reply = nullptr;
		emit finishedLoading(this, LoadResult::Error);
		return;
	}

	int first = m_smart && m_blim > 0 ? ((m_page - 1) * m_imagesPerPage) % m_blim : 0;

	// Parse source
	ParsedPage page = m_api->parsePage(m_parentPage, m_source, first, m_imagesPerPage);
	if (!page.error.isEmpty())
	{
		m_errors.append(page.error);
		log(QStringLiteral("[%1][%2] %3").arg(m_site->url(), m_format, page.error), Logger::Warning);
		m_reply->deleteLater();
		m_reply = nullptr;
		emit finishedLoading(this, LoadResult::Error);
		return;
	}

	// Fill data from parsing result
	if (page.imageCount >= 0)
	{ setImageCount(page.imageCount, true); }
	if (page.pageCount >= 0)
	{ setPageCount(page.pageCount, true); }
	for (const Tag &tag : qAsConst(page.tags))
	{ m_tags.append(tag); }
	for (const QSharedPointer<Image> &img : qAsConst(page.images))
	{ addImage(img); }
	if (page.urlNextPage.isValid())
	{ m_urlNextPage = page.urlNextPage; }
	if (page.urlPrevPage.isValid())
	{ m_urlPrevPage = page.urlPrevPage; }
	if (!page.wiki.isEmpty())
	{ m_wiki = page.wiki; }

	// Complete missing tag information from images' tags if necessary
	if (m_tags.isEmpty())
	{
		QStringList tagsGot;
		for (int i = 0; i < m_images.count(); i++)
		{
			QList<Tag> tags = m_images.at(i)->tags();
			for (int t = 0; t < tags.count(); t++)
			{
				if (tagsGot.contains(tags[t].text()))
				{ m_tags[tagsGot.indexOf(tags[t].text())].setCount(m_tags[tagsGot.indexOf(tags[t].text())].count()+1); }
				else
				{
					m_tags.append(tags[t]);
					tagsGot.append(tags[t].text());
				}
			}
		}
	}

	// Complete image count information from tag count information
	if (m_imagesCount < 1)
	{
		for (const Tag &tag : qAsConst(m_tags))
		{
			if (tag.text() == m_search.join(" "))
			{ setImageCount(tag.count(), false); }
		}
	}

	// Remove first n images (according to site settings)
	int skip = m_site->setting("ignore/always", 0).toInt();
	if (false && m_isAltPage) // FIXME(Bionus): broken since move to Api class
	{ skip = m_site->setting("ignore/alt", 0).toInt(); }
	if (m_page == 1)
	{ skip = m_site->setting("ignore/1", 0).toInt(); }
	if (m_api->getName() == QLatin1String("Html"))
	{
		if (m_images.size() >= skip)
		{
			for (int i = 0; i < skip; ++i)
				m_images.removeFirst();
		}
		else
		{ log(QStringLiteral("Wanting to skip %1 images but only %2 returned").arg(skip).arg(m_images.size()), Logger::Warning); }
	}

	// Virtual paging
	int firstImage = 0;
	int lastImage = m_smart ? m_imagesPerPage : m_images.size();
	if (false && !m_originalUrl.contains("{page}") && !m_originalUrl.contains("{cpage}") && !m_originalUrl.contains("{pagepart}") && !m_originalUrl.contains("{pid}")) // TODO(Bionus): add real virtual paging
	{
		firstImage = m_imagesPerPage * (m_page - 1);
		lastImage = m_imagesPerPage;
	}
	while (firstImage > 0 && !m_images.isEmpty())
	{
		m_images.removeFirst();
		firstImage--;
	}
	while (m_images.size() > lastImage)
	{ m_images.removeLast(); }

	log(QStringLiteral("[%1][%2] Parsed page <a href=\"%3\">%3</a>: %4 images, %5 tags (%6), %7 total (%8), %9 pages (%10)").arg(m_site->url(), m_format, m_reply->url().toString().toHtmlEscaped()).arg(m_images.count()).arg(page.tags.count()).arg(m_tags.count()).arg(imagesCount(false)).arg(imagesCount(true)).arg(pagesCount(false)).arg(pagesCount(true)), Logger::Info);

	m_reply->deleteLater();
	m_reply = nullptr;
	m_loaded = true;

	emit finishedLoading(this, LoadResult::Ok);
}