2018-12-28 12:55:11 +01:00
# include " DatabaseWindow.h "
2017-01-21 18:16:57 +01:00
# include "util.h"
2019-08-16 10:49:38 +02:00
# include "CrudTab.h"
# include "widgets/CatalogTablesPage.h"
2019-01-29 19:41:27 +01:00
# include "OpenDatabase.h"
2019-10-06 13:52:45 +02:00
# include "catalog/PgDatabaseCatalog.h"
2019-08-24 20:47:32 +02:00
# include "ConnectionController.h"
2017-02-01 18:01:02 +01:00
# include "MasterController.h"
2018-12-30 15:46:15 +01:00
# include "TaskExecutor.h"
2019-08-15 16:18:47 +02:00
# include <QAction>
2019-01-01 14:34:14 +01:00
# include <QApplication>
2020-04-04 13:23:43 +02:00
# include <QCloseEvent>
2019-08-15 16:18:47 +02:00
# include <QFileDialog>
# include <QMenuBar>
2019-01-01 14:34:14 +01:00
# include <QMessageBox>
# include <QMetaMethod>
2021-06-16 19:20:03 +02:00
# include <QMimeData>
2019-08-15 16:18:47 +02:00
# include <QStandardPaths>
2019-08-16 08:29:27 +02:00
# include <QStatusBar>
2019-01-06 08:17:37 +01:00
# include <QTableView>
# include "EditTableWidget.h"
# include "CodeGenerator.h"
2019-08-15 16:18:47 +02:00
# include "QueryTool.h"
2021-04-10 14:27:04 +02:00
# include <serverinspector/ServerInspector.h>
2017-01-08 15:16:16 +01:00
2016-12-26 16:06:55 +01:00
namespace pg = Pgsql ;
2019-01-05 09:49:12 +01:00
DatabaseWindow : : DatabaseWindow ( MasterController * master , QWidget * parent )
2019-08-15 16:18:47 +02:00
: QMainWindow ( parent )
2019-01-05 09:49:12 +01:00
, m_masterController ( master )
{
2018-12-30 15:46:15 +01:00
connect ( & loadWatcher , & QFutureWatcher < LoadCatalog : : Result > : : finished ,
this , & DatabaseWindow : : catalogLoaded ) ;
2019-01-01 11:15:16 +01:00
2019-08-15 16:18:47 +02:00
m_tabWidget = new QTabWidget ( this ) ;
2019-08-16 10:49:59 +02:00
m_tabWidget - > setObjectName ( " m_tabWidget " ) ;
2019-08-15 16:18:47 +02:00
setCentralWidget ( m_tabWidget ) ;
2021-07-08 16:28:32 +02:00
connect ( m_tabWidget , & QTabWidget : : tabCloseRequested , this , & DatabaseWindow : : m_tabWidget_tabCloseRequested ) ;
connect ( m_tabWidget , & QTabWidget : : currentChanged , this , & DatabaseWindow : : m_tabWidget_currentChanged ) ;
2019-08-15 16:18:47 +02:00
2021-07-08 16:28:32 +02:00
createActions ( ) ;
initMenus ( ) ;
2021-06-16 19:20:03 +02:00
setAcceptDrops ( true ) ;
2016-12-26 16:06:55 +01:00
}
2019-01-05 11:12:47 +01:00
DatabaseWindow : : ~ DatabaseWindow ( ) = default ;
2017-01-21 18:16:57 +01:00
2019-08-15 16:18:47 +02:00
void DatabaseWindow : : addPage ( QWidget * page , QString caption )
{
m_tabWidget - > addTab ( page , caption ) ;
m_tabWidget - > setCurrentWidget ( page ) ;
}
void DatabaseWindow : : setTabCaptionForWidget ( QWidget * widget , const QString & caption , const QString & hint )
{
auto index = m_tabWidget - > indexOf ( widget ) ;
m_tabWidget - > setTabText ( index , caption ) ;
m_tabWidget - > setTabToolTip ( index , hint ) ;
}
void DatabaseWindow : : setTabIcon ( QWidget * widget , const QString & iconname )
{
auto index = m_tabWidget - > indexOf ( widget ) ;
auto n = " :/icons/16x16/ " + iconname ;
m_tabWidget - > setTabIcon ( index , QIcon ( n ) ) ;
}
2018-12-28 12:55:11 +01:00
void DatabaseWindow : : newCodeGenPage ( QString query , std : : shared_ptr < const Pgsql : : Result > dbres )
2018-09-18 11:53:19 +02:00
{
2019-08-19 10:05:05 +02:00
auto cgtab = new CodeGenerator ( this ) ;
cgtab - > Init ( m_database - > catalog ( ) , query , dbres ) ;
addPage ( cgtab , " Codegen " ) ;
2019-08-15 19:32:33 +02:00
}
QueryTool * DatabaseWindow : : GetActiveQueryTool ( )
{
auto widget = m_tabWidget - > currentWidget ( ) ;
auto qt = dynamic_cast < QueryTool * > ( widget ) ;
return qt ;
2018-09-18 11:53:19 +02:00
}
2019-10-13 07:31:48 +02:00
CrudTab * DatabaseWindow : : GetActiveCrud ( )
{
auto widget = m_tabWidget - > currentWidget ( ) ;
auto ct = dynamic_cast < CrudTab * > ( widget ) ;
return ct ;
}
2020-04-04 13:23:43 +02:00
void DatabaseWindow : : closeEvent ( QCloseEvent * event )
{
for ( int idx = 0 ; idx < m_tabWidget - > count ( ) ; + + idx ) {
2021-07-05 20:41:24 +02:00
if ( ! canCloseTab ( idx ) ) {
event - > ignore ( ) ;
return ;
}
2020-04-04 13:23:43 +02:00
}
}
2018-12-28 12:55:11 +01:00
void DatabaseWindow : : setConfig ( const ConnectionConfig & config )
2017-01-15 21:01:40 +01:00
{
m_config = config ;
2017-12-19 18:17:41 +01:00
try {
QString title = " pglab - " ;
2019-09-16 19:24:39 +02:00
title + = m_config . name ( ) ;
2017-12-19 18:17:41 +01:00
setWindowTitle ( title ) ;
2018-12-30 15:46:15 +01:00
auto f = TaskExecutor : : run ( new LoadCatalog ( m_config ) ) ;
loadWatcher . setFuture ( f ) ;
2017-12-25 15:33:10 +01:00
} catch ( std : : runtime_error & ex ) {
QMessageBox : : critical ( this , " Error reading database " ,
QString : : fromUtf8 ( ex . what ( ) ) ) ;
close ( ) ;
}
}
2021-07-08 16:28:32 +02:00
QAction * DatabaseWindow : : createAction ( QString iconname , QString caption , void ( DatabaseWindow : : * func ) ( ) )
{
QIcon icon ;
icon . addFile ( iconname , QSize ( ) , QIcon : : Normal , QIcon : : On ) ;
QAction * action = new QAction ( icon , caption , this ) ;
connect ( action , & QAction : : triggered , this , func ) ;
return action ;
}
2019-08-15 16:18:47 +02:00
2021-07-08 16:28:32 +02:00
QAction * DatabaseWindow : : createAction ( QString caption , void ( DatabaseWindow : : * func ) ( ) )
2019-08-15 16:18:47 +02:00
{
2021-07-08 16:28:32 +02:00
QAction * action = new QAction ( caption , this ) ;
connect ( action , & QAction : : triggered , this , func ) ;
return action ;
}
2019-08-19 11:38:04 +02:00
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : createActions ( )
{
actionAbout = createAction ( " :/icons/about.png " , tr ( " About " ) , & DatabaseWindow : : actionAbout_triggered ) ;
actionCancelQuery = createAction ( " :/icons/script_delete.png " , tr ( " Cancel query " ) , & DatabaseWindow : : actionCancelQuery_triggered ) ;
actionClose = createAction ( " :/icons/page_white_delete.png " , tr ( " Close " ) , & DatabaseWindow : : actionClose_triggered ) ;
actionCopy = createAction ( " :/icons/page_white_copy.png " , tr ( " Copy " ) , & DatabaseWindow : : actionCopy_triggered ) ;
actionCopyAsCString = createAction ( " :/icons/token_shortland_character.png " , tr ( " Copy as C string " ) , & DatabaseWindow : : actionCopyAsCString_triggered ) ;
actionCopyAsRawCppString = createAction ( " :/icons/token_shortland_character.png " , tr ( " Copy as raw C++-string " ) , & DatabaseWindow : : actionCopyAsRawCppString_triggered ) ;
actionExecuteQuery = createAction ( " :/icons/script_go.png " , tr ( " Execute query " ) , & DatabaseWindow : : actionExecuteQuery_triggered ) ;
actionExplain = createAction ( " :/icons/lightbulb_off.png " , tr ( " Explain " ) , & DatabaseWindow : : actionExplain_triggered ) ;
actionExplainAnalyze = createAction ( " :/icons/lightbulb.png " , tr ( " Explain analyze " ) , & DatabaseWindow : : actionExplainAnalyze_triggered ) ;
actionExportData = createAction ( " :/icons/table_save.png " , tr ( " Export data " ) , & DatabaseWindow : : actionExportData_triggered ) ;
actionGenerateCode = createAction ( tr ( " Generate code " ) , & DatabaseWindow : : actionGenerateCode_triggered ) ;
actionInspectInformationSchema = createAction ( " :/icons/page_white_add.png " , tr ( " Inspect information_schema " ) , & DatabaseWindow : : actionInspectInformationSchema_triggered ) ;
actionInspectPgCatalog = createAction ( " :/icons/page_white_add.png " , tr ( " Inspect pg_catalog " ) , & DatabaseWindow : : actionInspectPgCatalog_triggered ) ;
actionInspectUserSchemas = createAction ( " :/icons/page_white_add.png " , tr ( " Inspect user schemas " ) , & DatabaseWindow : : actionInspectUserSchemas_triggered ) ;
actionServerInspector = createAction ( " :/icons/page_white_add.png " , tr ( " Inspect server " ) , & DatabaseWindow : : actionServerInspector_triggered ) ;
actionNewSql = createAction ( " :/icons/new_query_tab.png " , tr ( " New Query " ) , & DatabaseWindow : : actionNewSql_triggered ) ;
actionOpenSql = createAction ( " :/icons/folder.png " , tr ( " Open Query " ) , & DatabaseWindow : : actionOpenSql_triggered ) ;
actionSaveSql = createAction ( " :/icons/script_save.png " , tr ( " Save Query " ) , & DatabaseWindow : : actionSaveSql_triggered ) ;
actionPasteLangString = createAction ( tr ( " Paste lang string " ) , & DatabaseWindow : : actionPasteLangString_triggered ) ;
actionRefreshCatalog = createAction ( tr ( " Refresh " ) , & DatabaseWindow : : actionRefreshCatalog_triggered ) ;
actionRefreshCrud = createAction ( tr ( " Refresh " ) , & DatabaseWindow : : actionRefreshCrud_triggered ) ;
actionSaveSqlAs = createAction ( tr ( " Save query as " ) , & DatabaseWindow : : actionSaveSqlAs_triggered ) ;
actionSaveCopyOfSqlAs = createAction ( tr ( " Save copy of query as " ) , & DatabaseWindow : : actionSaveCopyOfSqlAs_triggered ) ;
actionShowConnectionManager = createAction ( tr ( " Show connection manager " ) , & DatabaseWindow : : actionShowConnectionManager_triggered ) ;
actionClose - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_W ) ) ;
actionCopy - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_C ) ) ;
actionCopyAsCString - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : SHIFT | Qt : : Key_C ) ) ;
actionExecuteQuery - > setShortcut ( QKeySequence ( Qt : : Key_F5 ) ) ;
actionExplain - > setShortcut ( QKeySequence ( Qt : : Key_F7 ) ) ;
actionExplainAnalyze - > setShortcut ( QKeySequence ( Qt : : SHIFT | Qt : : Key_F7 ) ) ;
actionNewSql - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_N ) ) ;
actionOpenSql - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_O ) ) ;
actionSaveSql - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_S ) ) ;
2019-08-15 16:18:47 +02:00
}
2019-08-15 19:32:33 +02:00
2019-08-15 16:18:47 +02:00
void DatabaseWindow : : initMenus ( )
{
auto mb = new QMenuBar ( this ) ;
menuFile = mb - > addMenu ( tr ( " File " ) ) ;
menuFile - > addActions ( {
2019-08-15 19:32:33 +02:00
actionNewSql ,
actionOpenSql ,
seperator ( ) ,
actionSaveSql ,
actionSaveSqlAs ,
actionSaveCopyOfSqlAs ,
seperator ( ) ,
actionExportData ,
seperator ( ) ,
actionClose
2019-08-15 16:18:47 +02:00
} ) ;
2019-08-15 20:15:13 +02:00
menuEdit = mb - > addMenu ( tr ( " Edit " ) ) ;
2019-08-15 19:32:33 +02:00
menuEdit - > addActions ( {
actionCopy ,
actionCopyAsCString ,
actionCopyAsRawCppString ,
2019-08-19 11:38:04 +02:00
// standard Paste missing Ctrl+V works however by default
actionPasteLangString ,
2019-08-15 19:32:33 +02:00
actionGenerateCode
} ) ;
2019-08-15 20:15:13 +02:00
2019-08-16 07:23:18 +02:00
menuQuery = mb - > addMenu ( tr ( " Query " ) ) ;
menuQuery - > addActions ( {
actionExecuteQuery ,
actionExplain ,
2019-08-16 08:27:05 +02:00
actionExplainAnalyze ,
actionCancelQuery
2019-08-16 07:23:18 +02:00
} ) ;
2019-10-06 13:52:45 +02:00
menuCatalog = mb - > addMenu ( tr ( " Catalog " ) ) ;
menuCatalog - > addActions ( {
actionRefreshCatalog
} ) ;
2019-10-13 07:31:48 +02:00
menuCrud = mb - > addMenu ( tr ( " CRUD " ) ) ;
menuCrud - > addActions ( {
actionRefreshCrud
} ) ;
2019-08-15 19:32:33 +02:00
menuWindow = mb - > addMenu ( tr ( " Window " ) ) ;
menuWindow - > addActions ( {
2019-08-15 20:15:13 +02:00
actionInspectUserSchemas ,
actionInspectPgCatalog ,
actionInspectInformationSchema ,
2021-04-10 14:27:04 +02:00
actionServerInspector ,
2019-08-15 20:15:13 +02:00
seperator ( ) ,
2019-08-15 19:32:33 +02:00
actionShowConnectionManager
} ) ;
2019-08-15 16:18:47 +02:00
menuHelp = mb - > addMenu ( tr ( " Help " ) ) ;
menuHelp - > addActions ( {
2019-08-15 19:32:33 +02:00
seperator ( ) ,
actionAbout
2019-08-15 16:18:47 +02:00
} ) ;
setMenuBar ( mb ) ;
}
2019-08-15 19:32:33 +02:00
QAction * DatabaseWindow : : seperator ( )
2019-08-15 16:18:47 +02:00
{
2019-08-15 19:32:33 +02:00
auto ac = new QAction ( this ) ;
ac - > setSeparator ( true ) ;
return ac ;
2019-08-15 16:18:47 +02:00
}
2019-10-13 07:31:48 +02:00
void DatabaseWindow : : newCreateTablePage ( )
{
auto w = new EditTableWidget ( m_database , this ) ;
m_tabWidget - > addTab ( w , " Create table " ) ;
}
void DatabaseWindow : : newCrudPage ( Oid tableoid )
{
CrudTab * ct = new CrudTab ( this , this ) ;
ct - > addAction ( actionRefreshCrud ) ;
addPage ( ct , " crud " ) ;
ct - > setConfig ( tableoid ) ;
}
void DatabaseWindow : : newCatalogInspectorPage ( QString caption , NamespaceFilter filter )
2019-08-16 10:49:38 +02:00
{
2021-09-29 19:39:42 +02:00
if ( ! m_database )
return ; // would be better if we queued the operation for later
2019-08-16 10:49:38 +02:00
auto ct = new CatalogInspector ( m_database , this ) ;
2019-10-13 07:31:48 +02:00
ct - > addAction ( actionRefreshCatalog ) ;
2019-08-16 10:49:38 +02:00
addPage ( ct , caption ) ;
ct - > setNamespaceFilter ( filter ) ;
2021-04-10 14:27:04 +02:00
connect ( ct - > tablesPage ( ) , & CatalogTablesPage : : tableSelected , this , & DatabaseWindow : : tableSelected ) ;
}
void DatabaseWindow : : newServerInspectorPage ( )
{
auto si = new ServerInspector ( m_database , this ) ;
addPage ( si , tr ( " Server " ) ) ;
2019-08-16 10:49:38 +02:00
}
2019-08-15 16:38:01 +02:00
2019-08-16 10:49:59 +02:00
void DatabaseWindow : : closeTab ( int index )
{
2021-07-04 20:24:05 +02:00
if ( index < 0 )
return ;
2019-08-16 10:49:59 +02:00
2021-09-17 18:55:10 +02:00
if ( canCloseTab ( index ) ) {
QWidget * widget = m_tabWidget - > widget ( index ) ;
2019-08-16 10:49:59 +02:00
m_tabWidget - > removeTab ( index ) ;
2021-09-17 18:55:10 +02:00
delete widget ;
}
2021-07-04 20:24:05 +02:00
}
bool DatabaseWindow : : canCloseTab ( int index ) const
{
QWidget * widget = m_tabWidget - > widget ( index ) ;
auto mp = dynamic_cast < ManagedPage * > ( widget ) ;
if ( mp ) {
return mp - > CanClose ( true ) ;
}
return true ;
2019-08-16 10:49:59 +02:00
}
2019-08-15 16:18:47 +02:00
2021-06-16 19:20:03 +02:00
void DatabaseWindow : : openSqlFile ( QString file_name )
{
if ( ! file_name . isEmpty ( ) ) {
auto * ct = new QueryTool ( this , this ) ;
if ( ct - > load ( file_name ) ) {
ct - > addAction ( actionExecuteQuery ) ;
addPage ( ct , ct - > title ( ) ) ;
}
else {
delete ct ;
}
}
}
2018-12-28 12:55:11 +01:00
void DatabaseWindow : : catalogLoaded ( )
2017-12-25 15:33:10 +01:00
{
2021-06-16 19:20:03 +02:00
try {
m_database = loadWatcher . future ( ) . result ( ) ;
// for (auto f : { "user", "pg_catalog", "information_schema" }) {
// // TODO open inspector windows
// }
// newCreateTablePage();
2021-07-08 16:28:32 +02:00
actionNewSql_triggered ( ) ;
2021-06-16 19:20:03 +02:00
} catch ( const OpenDatabaseException & ex ) {
QMessageBox : : critical ( this , " Error reading database " , ex . text ( ) ) ;
close ( ) ;
}
2017-01-15 21:01:40 +01:00
}
2019-08-16 10:49:38 +02:00
void DatabaseWindow : : tableSelected ( Oid tableoid )
{
newCrudPage ( tableoid ) ;
}
2019-08-15 19:32:33 +02:00
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionAbout_triggered ( )
2017-01-09 07:39:09 +01:00
{
2017-02-01 18:01:02 +01:00
QMessageBox : : about ( this , " pgLab 0.1 " , tr (
2019-01-05 11:12:47 +01:00
" Copyrights 2016-2019, Eelke Klein, All Rights Reserved. \n "
2017-02-01 18:01:02 +01:00
" \n "
2017-12-28 09:20:42 +01:00
" The program is provided AS IS with NO WARRANTY OF ANY KIND, "
" INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
2017-02-01 18:01:02 +01:00
" FOR A PARTICULAR PURPOSE. \n "
" \n "
2018-12-29 18:59:54 +01:00
" This program is dynamically linked with Qt 5.12 Copyright (C) 2018 "
2017-02-04 11:55:49 +01:00
" The Qt Company Ltd. https://www.qt.io/licensing/. \n "
2017-02-01 18:01:02 +01:00
" \n "
" Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons "
2017-12-19 19:55:12 +01:00
" attribution 3.0 license. "
2017-02-01 18:01:02 +01:00
) ) ;
2017-01-09 07:39:09 +01:00
}
2017-01-13 19:09:58 +01:00
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionCancelQuery_triggered ( )
2019-08-16 08:27:05 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
if ( query_tool ) {
query_tool - > cancel ( ) ;
}
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionClose_triggered ( )
2019-08-16 10:49:59 +02:00
{
m_tabWidget - > tabCloseRequested ( m_tabWidget - > currentIndex ( ) ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionCopy_triggered ( )
2017-02-04 11:55:49 +01:00
{
QWidget * w = QApplication : : focusWidget ( ) ;
2021-07-04 20:24:05 +02:00
if ( w = = nullptr )
return ;
QTableView * tv = dynamic_cast < QTableView * > ( w ) ;
if ( tv )
2017-02-04 11:55:49 +01:00
copySelectionToClipboard ( tv ) ;
2021-07-04 20:24:05 +02:00
else
InvokeCopyIfPresent ( w ) ;
}
void DatabaseWindow : : InvokeCopyIfPresent ( QWidget * w )
{
const QMetaObject * meta = w - > metaObject ( ) ;
int i = meta - > indexOfMethod ( " copy() " ) ;
if ( i ! = - 1 ) {
QMetaMethod method = meta - > method ( i ) ;
method . invoke ( w , Qt : : AutoConnection ) ;
}
2017-02-04 11:55:49 +01:00
}
2017-02-05 08:23:06 +01:00
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionCopyAsCString_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > copyQueryAsCString ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionCopyAsRawCppString_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > copyQueryAsRawCppString ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionExecuteQuery_triggered ( )
2019-08-16 07:23:18 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-16 07:23:18 +02:00
query_tool - > execute ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionExplain_triggered ( )
2019-08-16 07:23:18 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-16 07:23:18 +02:00
query_tool - > explain ( false ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionExplainAnalyze_triggered ( )
2019-08-16 07:23:18 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-16 07:23:18 +02:00
query_tool - > explain ( true ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionExportData_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > exportData ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionGenerateCode_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > generateCode ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionInspectInformationSchema_triggered ( )
2019-08-15 19:32:33 +02:00
{
2019-10-13 07:31:48 +02:00
newCatalogInspectorPage ( " information_schema " , NamespaceFilter : : InformationSchema ) ;
2019-08-15 19:32:33 +02:00
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionInspectPgCatalog_triggered ( )
2019-08-15 19:32:33 +02:00
{
2019-10-13 07:31:48 +02:00
newCatalogInspectorPage ( " pg_catalog " , NamespaceFilter : : PgCatalog ) ;
2019-08-15 19:32:33 +02:00
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionInspectUserSchemas_triggered ( )
2019-08-15 19:32:33 +02:00
{
2021-04-10 14:27:04 +02:00
newCatalogInspectorPage ( " Schema " , NamespaceFilter : : User ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionServerInspector_triggered ( )
2021-04-10 14:27:04 +02:00
{
newServerInspectorPage ( ) ;
2019-08-15 19:32:33 +02:00
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionNewSql_triggered ( )
2019-08-15 19:32:33 +02:00
{
2019-08-16 08:29:27 +02:00
auto * ct = new QueryTool ( this , this ) ;
2019-10-13 07:31:48 +02:00
ct - > addAction ( actionExecuteQuery ) ;
2019-12-03 19:08:13 +01:00
addPage ( ct , " new " ) ;
2019-08-15 19:32:33 +02:00
ct - > newdoc ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionOpenSql_triggered ( )
2019-08-15 19:32:33 +02:00
{
QString home_dir = QStandardPaths : : locate ( QStandardPaths : : HomeLocation , " " , QStandardPaths : : LocateDirectory ) ;
QString file_name = QFileDialog : : getOpenFileName ( this ,
tr ( " Open sql query " ) , home_dir , tr ( " SQL files (*.sql *.txt) " ) ) ;
2021-06-16 19:20:03 +02:00
openSqlFile ( file_name ) ;
2019-08-15 19:32:33 +02:00
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionPasteLangString_triggered ( )
2019-08-19 11:38:04 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-19 11:38:04 +02:00
query_tool - > pasteLangString ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionRefreshCatalog_triggered ( )
2019-10-06 13:52:45 +02:00
{
m_database - > refresh ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionRefreshCrud_triggered ( )
2019-10-13 07:31:48 +02:00
{
auto crud = GetActiveCrud ( ) ;
2021-07-04 20:24:05 +02:00
if ( crud )
2019-10-13 07:31:48 +02:00
crud - > refresh ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionSaveSql_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > save ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionSaveSqlAs_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > saveAs ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionSaveCopyOfSqlAs_triggered ( )
2019-08-15 19:32:33 +02:00
{
auto query_tool = GetActiveQueryTool ( ) ;
2021-07-04 20:24:05 +02:00
if ( query_tool )
2019-08-15 19:32:33 +02:00
query_tool - > saveCopyAs ( ) ;
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : actionShowConnectionManager_triggered ( )
2019-08-15 19:32:33 +02:00
{
2019-08-19 10:05:05 +02:00
m_masterController - > connectionController ( ) - > showConnectionManager ( ) ;
2019-08-15 19:32:33 +02:00
}
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : m_tabWidget_tabCloseRequested ( int index )
2019-08-16 10:49:59 +02:00
{
closeTab ( index ) ;
}
2019-08-16 08:29:27 +02:00
2021-07-08 16:28:32 +02:00
void DatabaseWindow : : m_tabWidget_currentChanged ( int )
2019-10-13 07:31:48 +02:00
{
auto widget = m_tabWidget - > currentWidget ( ) ;
auto qt = dynamic_cast < QueryTool * > ( widget ) ;
auto ct = dynamic_cast < CrudTab * > ( widget ) ;
auto ci = dynamic_cast < CatalogInspector * > ( widget ) ;
menuQuery - > menuAction ( ) - > setVisible ( qt ! = nullptr ) ;
menuCatalog - > menuAction ( ) - > setVisible ( ci ! = nullptr ) ;
menuCrud - > menuAction ( ) - > setVisible ( ct ! = nullptr ) ;
}
2019-08-16 08:29:27 +02:00
void DatabaseWindow : : setTitleForWidget ( QWidget * widget , QString title , QString hint )
{
int i = m_tabWidget - > indexOf ( widget ) ;
if ( i > = 0 ) {
m_tabWidget - > setTabText ( i , title ) ;
m_tabWidget - > setTabToolTip ( i , hint ) ;
}
}
void DatabaseWindow : : setIconForWidget ( QWidget * widget , QIcon icon )
{
int i = m_tabWidget - > indexOf ( widget ) ;
2021-07-04 20:24:05 +02:00
if ( i > = 0 )
2019-08-16 08:29:27 +02:00
m_tabWidget - > setTabIcon ( i , icon ) ;
}
std : : shared_ptr < OpenDatabase > DatabaseWindow : : openDatabase ( )
{
return m_database ;
}
void DatabaseWindow : : showStatusBarMessage ( QString message )
{
2021-06-16 19:20:03 +02:00
statusBar ( ) - > showMessage ( message ) ;
}
void DatabaseWindow : : dragEnterEvent ( QDragEnterEvent * event )
{
2021-07-04 20:24:05 +02:00
if ( event - > mimeData ( ) - > hasUrls ( ) )
2021-06-16 19:20:03 +02:00
event - > acceptProposedAction ( ) ;
}
void DatabaseWindow : : dropEvent ( QDropEvent * event )
{
foreach ( const QUrl & url , event - > mimeData ( ) - > urls ( ) ) {
QString file_name = url . toLocalFile ( ) ;
qDebug ( ) < < " Dropped file: " < < file_name ;
openSqlFile ( file_name ) ;
}
2019-08-16 08:29:27 +02:00
}