Beispiel #1
0
void Document::loadTheme(const Theme& theme)
{
	m_text->document()->blockSignals(true);

	// Update colors
	QColor color = theme.foregroundColor();
	color.setAlpha(theme.foregroundOpacity() * 2.55f);
	m_text->setStyleSheet(
		QString("QTextEdit { background: rgba(%1, %2, %3, %4); color: %5; selection-background-color: %6; selection-color: %7; padding: %8px; }")
			.arg(color.red())
			.arg(color.green())
			.arg(color.blue())
			.arg(color.alpha())
			.arg(theme.textColor().name())
			.arg(theme.textColor().name())
			.arg(theme.foregroundColor().name())
			.arg(theme.foregroundPadding())
	);
	if (m_highlighter->misspelledColor() != theme.misspelledColor()) {
		m_highlighter->setMisspelledColor(theme.misspelledColor());
	}

	// Update text
	QFont font = theme.textFont();
	font.setStyleStrategy(m_text->font().styleStrategy());
	if (m_text->font() != font) {
		m_text->setFont(font);
	}
	m_text->setCursorWidth(!m_block_cursor ? 1 : m_text->fontMetrics().averageCharWidth());

	int margin = theme.foregroundMargin();
	m_layout->setColumnMinimumWidth(0, margin);
	m_layout->setColumnMinimumWidth(2, margin);
	if (theme.foregroundPosition() < 3) {
		m_text->setFixedWidth(theme.foregroundWidth());

		switch (theme.foregroundPosition()) {
		case 0:
			m_layout->setColumnStretch(0, 0);
			m_layout->setColumnStretch(2, 1);
			break;

		case 2:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 0);
			break;

		case 1:
		default:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 1);
			break;
		};
	} else {
		m_text->setMinimumWidth(theme.foregroundWidth());
		m_text->setMaximumWidth(maximumSize().height());

		m_layout->setColumnStretch(0, 0);
		m_layout->setColumnStretch(2, 0);
	}

	centerCursor(true);
	m_text->document()->blockSignals(false);
}
Beispiel #2
0
void Document::loadTheme(const Theme& theme)
{
	m_text->document()->blockSignals(true);

        m_block_default_format.clear();
        for(int i=0;i<theme.definedDefaultFormatsForBlocks().length();i++)
            m_block_default_format.insert(theme.definedDefaultFormatsForBlocks()[i],theme.defaultFormatForBlock(theme.definedDefaultFormatsForBlocks()[i]));

	// Update colors
	QString contrast = (qGray(theme.textColor().rgb()) > 127) ? "black" : "white";
	QColor color = theme.foregroundColor();
	color.setAlpha(theme.foregroundOpacity() * 2.55f);
        m_text->setStyleSheet(
                QString("Editor { background: rgba(%1, %2, %3, %4); color: %5; selection-background-color: %6; selection-color: %7; padding: %8px; border-radius: %9px; }")
			.arg(color.red())
			.arg(color.green())
			.arg(color.blue())
			.arg(color.alpha())
			.arg(theme.textColor().name())
			.arg(theme.textColor().name())
			.arg(contrast)
			.arg(theme.foregroundPadding())
			.arg(theme.foregroundRounding())
	);
	if (m_highlighter->misspelledColor() != theme.misspelledColor()) {
		m_highlighter->setMisspelledColor(theme.misspelledColor());
	}

	// Update text
	QFont font = theme.textFont();
	font.setStyleStrategy(m_text->font().styleStrategy());
	if (m_text->font() != font) {
		m_text->setFont(font);
	}
	m_text->setCursorWidth(!m_block_cursor ? 1 : m_text->fontMetrics().averageCharWidth());

	int margin = theme.foregroundMargin();
	m_layout->setColumnMinimumWidth(0, margin);
	m_layout->setColumnMinimumWidth(2, margin);
	if (theme.foregroundPosition() < 3) {
		m_text->setFixedWidth(theme.foregroundWidth());

		switch (theme.foregroundPosition()) {
		case 0:
			m_layout->setColumnStretch(0, 0);
			m_layout->setColumnStretch(2, 1);
			break;

		case 2:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 0);
			break;

		case 1:
		default:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 1);
			break;
		};
	} else {
		m_text->setMinimumWidth(theme.foregroundWidth());
		m_text->setMaximumWidth(maximumSize().height());

		m_layout->setColumnStretch(0, 0);
		m_layout->setColumnStretch(2, 0);
	}
        cleanUpDocument(false);
	centerCursor(true);
	m_text->document()->blockSignals(false);
        m_highlighter->rehighlight();
}
Beispiel #3
0
void ThemeDialog::renderPreview(QImage preview, const QRect& foreground, const Theme& theme)
{
	m_load_color = QtConcurrent::run(averageImage, theme.backgroundImage(), theme.backgroundColor());

	// Position preview text
	int padding = theme.foregroundPadding();
	int x = foreground.x() + padding;
	int y = foreground.y() + padding + theme.spacingAboveParagraph();
	int width = foreground.width() - (padding * 2);
	int height = foreground.height() - (padding * 2) - theme.spacingAboveParagraph();
	m_preview_text->setGeometry(x, y, width, height);

	// Set colors
	QColor text_color = theme.textColor();
	text_color.setAlpha(255);

	QPalette p = m_preview_text->palette();
	p.setBrush(QPalette::Base, preview.copy(x, y, width, height));
	p.setColor(QPalette::Text, text_color);
	p.setColor(QPalette::Highlight, text_color);
	p.setColor(QPalette::HighlightedText, (qGray(text_color.rgb()) > 127) ? Qt::black : Qt::white);
	m_preview_text->setPalette(p);

	// Set spacings
	int tab_width = theme.tabWidth();
	QTextBlockFormat block_format;
	block_format.setLineHeight(theme.lineSpacing(), (theme.lineSpacing() == 100) ? QTextBlockFormat::SingleHeight : QTextBlockFormat::ProportionalHeight);
	block_format.setTextIndent(tab_width * theme.indentFirstLine());
	block_format.setTopMargin(theme.spacingAboveParagraph());
	block_format.setBottomMargin(theme.spacingBelowParagraph());
	m_preview_text->textCursor().mergeBlockFormat(block_format);
	for (int i = 0, count = m_preview_text->document()->allFormats().count(); i < count; ++i) {
		QTextFormat& f = m_preview_text->document()->allFormats()[i];
		if (f.isBlockFormat()) {
			f.merge(block_format);
		}
	}
	m_preview_text->setTabStopWidth(tab_width);
	m_preview_text->document()->setIndentWidth(tab_width);

	// Set font
	m_preview_text->setFont(theme.textFont());

	// Render text
	m_preview_text->render(&preview, m_preview_text->pos());

	// Create zoomed text cutout
	int x2 = (x >= 24) ? (x - 24) : 0;
	int y2 = (y >= 24) ? (y - 24) : 0;
	QImage text_cutout = preview.copy(x2, y2, 162, 110);

	// Create preview icon
	m_preview_icon = QImage(":/shadow.png").convertToFormat(QImage::Format_ARGB32_Premultiplied);
	{
		QPainter painter(&m_preview_icon);
		painter.drawImage(9, 9, preview.scaled(240, 135, Qt::KeepAspectRatio, Qt::SmoothTransformation));
		painter.fillRect(20, 32, 85, 59, QColor(0, 0, 0, 32));
		painter.fillRect(21, 33, 83, 57, Qt::white);
		int x3 = (x >= 24) ? (x - 12 + theme.tabWidth()) : 12 + theme.tabWidth();
		int y3 = (y >= 24) ? (y - 6) : 0;
		painter.drawImage(22, 34, preview, x3, y3, 81, 55);
	}

	// Create preview pixmap
	preview = preview.scaled(480, 270, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	{
		QPainter painter(&preview);
		painter.setPen(Qt::NoPen);
		painter.fillRect(22, 46, 170, 118, QColor(0, 0, 0, 32));
		painter.fillRect(24, 48, 166, 114, Qt::white);
		painter.drawImage(26, 50, text_cutout);
	}
	m_preview->setPixmap(QPixmap::fromImage(preview));
}