/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqlayout.h>
#include <tqframe.h>
#include <tqscrollbar.h>

#include <ZLibrary.h>
#include <ZLLanguageUtil.h>

#include "ZLTQtViewWidget.h"
#include "ZLTQtPaintContext.h"

ZLTQtViewWidget::ZLTQtViewWidgetInternal::ZLTQtViewWidgetInternal(TQWidget *parent, ZLTQtViewWidget &holder) : TQWidget(parent), myHolder(holder) {
	setBackgroundMode(NoBackground);
	setFocusPolicy(ClickFocus);
}

TQScrollBar *ZLTQtViewWidget::addScrollBar(TQGridLayout *layout, TQt::Orientation orientation, int x, int y) {
	TQScrollBar *scrollBar = new TQScrollBar(orientation, myFrame);
	layout->addWidget(scrollBar, x, y);
	scrollBar->hide();
	if (orientation == TQt::Vertical) {
		connect(scrollBar, TQ_SIGNAL(sliderMoved(int)), this, TQ_SLOT(onVerticalSliderMoved(int)));
		connect(scrollBar, TQ_SIGNAL(nextLine()), this, TQ_SLOT(onVerticalSliderStepNext()));
		connect(scrollBar, TQ_SIGNAL(nextPage()), this, TQ_SLOT(onVerticalSliderPageNext()));
		connect(scrollBar, TQ_SIGNAL(prevLine()), this, TQ_SLOT(onVerticalSliderStepPrevious()));
		connect(scrollBar, TQ_SIGNAL(prevPage()), this, TQ_SLOT(onVerticalSliderPagePrevious()));
	} else {
		connect(scrollBar, TQ_SIGNAL(sliderMoved(int)), this, TQ_SLOT(onHorizontalSliderMoved(int)));
		connect(scrollBar, TQ_SIGNAL(nextLine()), this, TQ_SLOT(onHorizontalSliderStepNext()));
		connect(scrollBar, TQ_SIGNAL(nextPage()), this, TQ_SLOT(onHorizontalSliderPageNext()));
		connect(scrollBar, TQ_SIGNAL(prevLine()), this, TQ_SLOT(onHorizontalSliderStepPrevious()));
		connect(scrollBar, TQ_SIGNAL(prevPage()), this, TQ_SLOT(onHorizontalSliderPagePrevious()));
	}
	return scrollBar;
}

ZLTQtViewWidget::ZLTQtViewWidget(TQWidget *parent, ZLApplication *application, const ZLTQtViewWidgetPositionInfo &positionInfo) : ZLViewWidget((ZLView::Angle)application->AngleStateOption.value()), myApplication(application), myPositionInfo(positionInfo) {
	myFrame = new TQFrame(parent);
	TQGridLayout *layout = new TQGridLayout(myFrame, 3, 3);
	layout->setRowStretch(0, 0);
	layout->setRowStretch(1, 1);
	layout->setRowStretch(2, 0);
	layout->setColStretch(0, 0);
	layout->setColStretch(1, 1);
	layout->setColStretch(2, 0);
	myTQWidget = new ZLTQtViewWidgetInternal(myFrame, *this);
  layout->addWidget(myTQWidget, 1, 1);

	myRightScrollBar = addScrollBar(layout, TQt::Vertical, 1, 2);
	myLeftScrollBar = addScrollBar(layout, TQt::Vertical, 1, 0);
	myShowScrollBarAtRight = true;

	myBottomScrollBar = addScrollBar(layout, TQt::Horizontal, 2, 1);
	myTopScrollBar = addScrollBar(layout, TQt::Horizontal, 0, 1);
	myShowScrollBarAtBottom = true;
}

const ZLTQtViewWidgetPositionInfo &ZLTQtViewWidget::positionInfo() const {
	return myPositionInfo;
}

void ZLTQtViewWidget::trackStylus(bool track) {
	myTQWidget->setMouseTracking(track);
}

void ZLTQtViewWidget::ZLTQtViewWidgetInternal::paintEvent(TQPaintEvent*) {
	ZLTQtPaintContext &context = (ZLTQtPaintContext&)myHolder.view()->context();
	switch (myHolder.rotation()) {
		default:
			context.setSize(width(), height());
			break;
		case ZLView::DEGREES90:
		case ZLView::DEGREES270:
			context.setSize(height(), width());
			break;
	}
	myHolder.view()->paint();
	TQPainter realPainter(this);
	switch (myHolder.rotation()) {
		default:
			realPainter.drawPixmap(0, 0, context.pixmap());
			break;
		case ZLView::DEGREES90:
			realPainter.rotate(270);
			realPainter.drawPixmap(1 - height(), -1, context.pixmap());
			break;
		case ZLView::DEGREES180:
			realPainter.rotate(180);
			realPainter.drawPixmap(1 - width(), 1 - height(), context.pixmap());
			break;
		case ZLView::DEGREES270:
			realPainter.rotate(90);
			realPainter.drawPixmap(0, 1 - width(), context.pixmap());
			break;
	}
}

void ZLTQtViewWidget::ZLTQtViewWidgetInternal::mousePressEvent(TQMouseEvent *event) {
	myHolder.view()->onStylusMove(x(event), y(event));
	myHolder.view()->onStylusPress(x(event), y(event));
}

void ZLTQtViewWidget::ZLTQtViewWidgetInternal::mouseReleaseEvent(TQMouseEvent *event) {
	myHolder.view()->onStylusRelease(x(event), y(event));
}

void ZLTQtViewWidget::ZLTQtViewWidgetInternal::mouseMoveEvent(TQMouseEvent *event) {
	switch (event->state() & MouseButtonMask) {
		case LeftButton:
			myHolder.view()->onStylusMovePressed(x(event), y(event));
			break;
		case NoButton:
			myHolder.view()->onStylusMove(x(event), y(event));
			break;
		default:
			break;
	}
}

int ZLTQtViewWidget::ZLTQtViewWidgetInternal::x(const TQMouseEvent *event) const {
	const int maxX = width() - 1;
	const int maxY = height() - 1;
	switch (myHolder.rotation()) {
		default:
			return std::min(std::max(event->x(), 0), maxX);
		case ZLView::DEGREES90:
			return maxY - std::min(std::max(event->y(), 0), maxY);
		case ZLView::DEGREES180:
			return maxX - std::min(std::max(event->x(), 0), maxX);
		case ZLView::DEGREES270:
			return std::min(std::max(event->y(), 0), maxY);
	}
}

int ZLTQtViewWidget::ZLTQtViewWidgetInternal::y(const TQMouseEvent *event) const {
	const int maxX = width() - 1;
	const int maxY = height() - 1;
	switch (myHolder.rotation()) {
		default:
			return std::min(std::max(event->y(), 0), maxY);
		case ZLView::DEGREES90:
			return std::min(std::max(event->x(), 0), maxX);
		case ZLView::DEGREES180:
			return maxY - std::min(std::max(event->y(), 0), maxY);
		case ZLView::DEGREES270:
			return maxX - std::min(std::max(event->x(), 0), maxX);
	}
}

void ZLTQtViewWidget::repaint()	{
	myTQWidget->repaint(false);
}

void ZLTQtViewWidget::ZLTQtViewWidgetInternal::resizeEvent(TQResizeEvent*) {
	/*
	const ZLTQtViewWidgetPositionInfo &info = myHolder.positionInfo();

	const int x = info.x();
	const int y = info.y();
	const TQPoint position = pos();
	if ((x != position.x()) || (y != position.y())) {
		move(x, y);
	}

	const int width = info.width();
	const int height = info.height();
	if ((width != this->width()) || (height != this->height())) {
		resize(width, height);
	}
	*/
}

void ZLTQtViewWidget::setScrollbarEnabled(ZLView::Direction direction, bool enabled) {
	if (direction == ZLView::VERTICAL) {
		myRightScrollBar->setShown(enabled && myShowScrollBarAtRight);
		myLeftScrollBar->setShown(enabled && !myShowScrollBarAtRight);
	} else {
		myBottomScrollBar->setShown(enabled && myShowScrollBarAtBottom);
		myTopScrollBar->setShown(enabled && !myShowScrollBarAtBottom);
	}
}

void ZLTQtViewWidget::setScrollbarPlacement(ZLView::Direction direction, bool standard) {
	if ((rotation() == ZLView::DEGREES90) || (rotation() == ZLView::DEGREES270)) {
		if (ZLLanguageUtil::isRTLLanguage(ZLibrary::Language())) {
			standard = !standard;
		}
	}
	if (direction == ZLView::VERTICAL) {
		if (standard != myShowScrollBarAtRight) {
			myShowScrollBarAtRight = standard;
			TQScrollBar *old = standard ? myLeftScrollBar : myRightScrollBar;
			TQScrollBar *current = standard ? myRightScrollBar : myLeftScrollBar;
			if (old->isShown()) {
				old->hide();
				current->show();
			}
		}
	} else {
		if (standard != myShowScrollBarAtBottom) {
			myShowScrollBarAtBottom = standard;
			TQScrollBar *old = standard ? myTopScrollBar : myBottomScrollBar;
			TQScrollBar *current = standard ? myBottomScrollBar : myTopScrollBar;
			if (old->isShown()) {
				old->hide();
				current->show();
			}
		}
	}
}

void ZLTQtViewWidget::setScrollbarParameters(ZLView::Direction direction, size_t full, size_t from, size_t to) {
	TQScrollBar *bar =
		(direction == ZLView::VERTICAL) ?  verticalScrollBar() : horizontalScrollBar();
	bar->setMinValue(0);
	bar->setMaxValue(full + from - to);
	bar->setValue(from);
	bar->setPageStep(to - from);
}

TQWidget *ZLTQtViewWidget::widget() const {
	return myFrame;
}

void ZLTQtViewWidget::onVerticalSliderMoved(int value) {
	TQScrollBar *bar = verticalScrollBar();
	size_t maxValue = bar->maxValue();
	size_t pageStep = bar->pageStep();
	onScrollbarMoved(
		ZLView::VERTICAL,
		maxValue + pageStep,
		(size_t)value,
		(size_t)value + pageStep
	);
}

void ZLTQtViewWidget::onVerticalSliderStepNext() {
	onScrollbarStep(ZLView::VERTICAL, 1);
}

void ZLTQtViewWidget::onVerticalSliderStepPrevious() {
	onScrollbarStep(ZLView::VERTICAL, -1);
}

void ZLTQtViewWidget::onVerticalSliderPageNext() {
	onScrollbarPageStep(ZLView::VERTICAL, 1);
}

void ZLTQtViewWidget::onVerticalSliderPagePrevious() {
	onScrollbarPageStep(ZLView::VERTICAL, -1);
}

void ZLTQtViewWidget::onHorizontalSliderMoved(int value) {
	TQScrollBar *bar = horizontalScrollBar();
	size_t maxValue = bar->maxValue();
	size_t pageStep = bar->pageStep();
	onScrollbarMoved(
		ZLView::HORIZONTAL,
		maxValue + pageStep,
		(size_t)value,
		(size_t)value + pageStep
	);
}

void ZLTQtViewWidget::onHorizontalSliderStepNext() {
	onScrollbarStep(ZLView::HORIZONTAL, 1);
}

void ZLTQtViewWidget::onHorizontalSliderStepPrevious() {
	onScrollbarStep(ZLView::HORIZONTAL, -1);
}

void ZLTQtViewWidget::onHorizontalSliderPageNext() {
	onScrollbarPageStep(ZLView::HORIZONTAL, 1);
}

void ZLTQtViewWidget::onHorizontalSliderPagePrevious() {
	onScrollbarPageStep(ZLView::HORIZONTAL, -1);
}

TQScrollBar *ZLTQtViewWidget::verticalScrollBar() {
	return myShowScrollBarAtRight ? myRightScrollBar : myLeftScrollBar;
}

TQScrollBar *ZLTQtViewWidget::horizontalScrollBar() {
	return myShowScrollBarAtBottom ? myBottomScrollBar : myTopScrollBar;
}

#include "ZLTQtViewWidget.moc"
