/****************************************************************************
** Dooble - The Secure Internet Web Browser
**
** Copyright (c) 2008, 2009, 2010, 2011 Alexis Megas,
** Gunther van Dooble, and the Dooble Team.
** All rights reserved.
**
** License: GPL2 only:
** 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; version 2 of the License only.
**
** 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
** or see here: http://www.gnu.org/licenses/gpl.html
**
** For the WebKit library, please see: http://webkit.org.
**
** THE CODE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
** IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS APPLICATION, EVEN IF ADVISED
** OF THE POSSIBILITY OF SUCH DAMAGE.
**
** Please report all praise, requests, bugs, and problems to the project
** team and administrators: http://sf.net/projects/dooble.
**
** You can find us listed at our project page. New team members are welcome.
** The name of the authors should not be used to endorse or promote products
** derived from Dooble without specific prior written permission.
** If you use this code for other projects, please let us know.
**
** Web sites:
**   http://sf.net/projects/dooble
**   http://dooble.sf.net
****************************************************************************/

#include <QMenu>
#include <QTabBar>
#include <QSettings>
#include <QToolButton>

#include "dooble.h"
#include "dtabwidget.h"

dtabwidget::dtabwidget(QWidget *parent):QTabWidget(parent)
{
#ifdef Q_WS_MAC
  setDocumentMode(true);
#endif
  setElideMode(Qt::ElideRight);
  selectedTabIndex = -1;
  setStyleSheet("QTabWidget::tab-bar {"
		"alignment: left; "
		"}");
  tabBar()->setStyleSheet
    ("QTabBar::tear {"
     "image: none; "
     "} ");
  tabBar()->setStyleSheet(tabBar()->styleSheet() +
			  "QTabBar::tab {"
			  "max-width: 225px; "
			  "min-width: 225px; "
#ifdef Q_WS_MAC
			  // Nothing?
#elif defined Q_WS_WIN
			  // Nothing?
#else
			  "max-height: 4ex; "
			  "min-height: 4ex; "
#endif
			  "}");
  tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(tabBar(), SIGNAL(customContextMenuRequested(const QPoint &)),
	  this,
	  SLOT(slotShowContextMenu(const QPoint &)));
  connect(tabBar(), SIGNAL(tabMoved(int, int)),
	  this,
	  SIGNAL(tabMoved(int, int)));
  setMovable(true);
  setTabsClosable(true);
  connect(this,
	  SIGNAL(tabCloseRequested(int)),
	  this,
	  SLOT(slotCloseTab(int)));
  addTabButton = new QToolButton();
  removeTabButton = new QToolButton();
  addTabButton->setAutoRaise(true);
  addTabButton->setToolTip(tr("Add New Page"));
  connect(addTabButton,
	  SIGNAL(clicked(void)),
	  this,
	  SLOT(slotCreateTab(void)));
  setCornerWidget(addTabButton, Qt::TopLeftCorner);
  removeTabButton = new QToolButton();
  removeTabButton->setAutoRaise(true);
  removeTabButton->setToolTip(tr("Remove Current Page"));
  setCornerWidget(removeTabButton, Qt::TopRightCorner);
  removeTabButton->setEnabled(false);
  connect(removeTabButton,
	  SIGNAL(clicked(bool)),
	  this,
	  SLOT(slotCloseTab(bool)));
  slotSetIcons();
}

dtabwidget::~dtabwidget()
{
}

void dtabwidget::slotShowContextMenu(const QPoint &point)
{
  selectedTabIndex = tabBar()->tabAt(point);

  if(selectedTabIndex > -1)
    {
      if(tabData(selectedTabIndex).toString() != "Desktop")
	{
	  QMenu menu(this);
	  QAction *action = 0;

	  action = menu.addAction(tr("Close Ta&b"),
				  this, SLOT(slotCloseTab(void)));
	  action->setEnabled(count() != 1);
	  action = menu.addAction(tr("Close &Other Tabs"),
				  this, SLOT(slotCloseOtherTabs(void)));
	  action->setEnabled(count() > 1);
	  menu.addSeparator();
	  menu.addAction(tr("New &Tab"),
			 this, SLOT(slotCreateTab(void)));
	  action = menu.addAction(tr("Open In &New Window"),
				  this, SLOT(slotOpenInNewWindow(void)));
	  action->setEnabled(count() > 1);
	  menu.exec(tabBar()->mapToGlobal(point));
	}
      else
	{
	  QMenu menu(this);
	  QAction *action = 0;

	  action = menu.addAction(tr("Close Ta&b"),
				  this, SLOT(slotCloseTab(void)));
	  action->setEnabled(count() != 1);
	  action = menu.addAction(tr("Close &Other Tabs"),
				  this, SLOT(slotCloseOtherTabs(void)));
	  action->setEnabled(count() > 1);
	  menu.addSeparator();
	  menu.addAction(tr("New &Tab"),
			 this, SLOT(slotCreateTab(void)));
	  menu.exec(tabBar()->mapToGlobal(point));
	}
    }
}

void dtabwidget::slotCloseTab(int index)
{
  if(index >= 0 && index < count())
    {
      selectedTabIndex = index;
      emit closeTab(selectedTabIndex);
    }
}

void dtabwidget::slotCloseTab(bool checked)
{
  Q_UNUSED(checked);
  emit closeTab(currentIndex());
}

void dtabwidget::slotCloseTab(void)
{
  emit closeTab(selectedTabIndex);
}

void dtabwidget::slotCreateTab(void)
{
  emit createTab();
}

void dtabwidget::slotOpenInNewWindow(void)
{
  emit openInNewWindow(selectedTabIndex);
}

void dtabwidget::tabRemoved(int index)
{
  Q_UNUSED(index);
}

void dtabwidget::setTabData(const int index, const QVariant &data)
{
  if(index >= 0 && index < count())
    tabBar()->setTabData(index, data);
}

QVariant dtabwidget::tabData(const int index) const
{
  return tabBar()->tabData(index);
}

void dtabwidget::setBarVisible(const bool state)
{
  tabBar()->setVisible(state);
}

void dtabwidget::slotSetIcons(void)
{
  QSettings settings
    (dooble::s_settings.value("iconSet").toString(), QSettings::IniFormat);

  settings.beginGroup("mainWindow");
  addTabButton->setIcon(QIcon(settings.value("addTab").toString()));
  removeTabButton->setIcon(QIcon(settings.value("removeTab").toString()));
}

void dtabwidget::mouseDoubleClickEvent(QMouseEvent *event)
{
  if(event && event->button() == Qt::LeftButton)
    {
      if(dooble::s_settings.value("settingsWindow/addTabWithDoubleClick",
				  true).toBool())
	emit createTab();
    }

  QTabWidget::mouseDoubleClickEvent(event);
}

void dtabwidget::mousePressEvent(QMouseEvent *event)
{
  if(event && event->button() == Qt::MidButton)
    {
      if(!dooble::s_settings.value("settingsWindow/closeViaMiddle",
				   true).toBool())
	return;

      int index = tabBar()->tabAt(event->pos());

      if(index != -1)
	emit closeTab(index);
    }

  QTabWidget::mousePressEvent(event);
}

void dtabwidget::slotCloseOtherTabs(void)
{
  int index = selectedTabIndex;

  for(int i = 0; i < count(); i++)
    if(i != index)
      slotCloseTab(i);
}

