summaryrefslogtreecommitdiff
path: root/dbaccess/source/ui/querydesign/QueryTextView.cxx
blob: f8d8471e53e329a81a2bd12dd80b921cfba18164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svx/svxids.hrc>
#include <QueryTextView.hxx>
#include <querycontainerwindow.hxx>
#include <helpids.h>
#include <querycontroller.hxx>
#include <sqledit.hxx>
#include <undosqledit.hxx>

using namespace dbaui;

// end of temp classes
OQueryTextView::OQueryTextView(OQueryContainerWindow* pParent, OQueryController& rController)
    : InterimItemWindow(pParent, "dbaccess/ui/queryview.ui", "QueryView")
    , m_rController(rController)
    , m_xSQL(new SQLEditView(m_xBuilder->weld_scrolled_window("scrolledwindow", true)))
    , m_xSQLEd(new weld::CustomWeld(*m_xBuilder, "sql", *m_xSQL))
    , m_timerUndoActionCreation("dbaccess OQueryTextView m_timerUndoActionCreation")
    , m_timerInvalidate("dbaccess OQueryTextView m_timerInvalidate")
    , m_bStopTimer(false)
{
    m_xSQL->DisableInternalUndo();
    m_xSQL->SetHelpId(HID_CTL_QRYSQLEDIT);
    m_xSQL->SetModifyHdl(LINK(this, OQueryTextView, ModifyHdl));
    m_xSQL->SetAcceptsTab(true);

    m_timerUndoActionCreation.SetTimeout(1000);
    m_timerUndoActionCreation.SetInvokeHandler(LINK(this, OQueryTextView, OnUndoActionTimer));

    m_timerInvalidate.SetTimeout(200);
    m_timerInvalidate.SetInvokeHandler(LINK(this, OQueryTextView, OnInvalidateTimer));
    m_timerInvalidate.Start();
}

IMPL_LINK_NOARG(OQueryTextView, ModifyHdl, LinkParamNone*, void)
{
    if (m_timerUndoActionCreation.IsActive())
        m_timerUndoActionCreation.Stop();
    m_timerUndoActionCreation.Start();

    if (!m_rController.isModified())
        m_rController.setModified(true);

    m_rController.InvalidateFeature(SID_SBA_QRY_EXECUTE);
    m_rController.InvalidateFeature(SID_CUT);
    m_rController.InvalidateFeature(SID_COPY);
}

IMPL_LINK_NOARG(OQueryTextView, OnUndoActionTimer, Timer*, void)
{
    OUString aText = m_xSQL->GetText();
    if (aText == m_strOrigText)
        return;

    SfxUndoManager& rUndoMgr = m_rController.GetUndoManager();
    std::unique_ptr<OSqlEditUndoAct> xUndoAct(new OSqlEditUndoAct(*this));

    xUndoAct->SetOriginalText(m_strOrigText);
    rUndoMgr.AddUndoAction(std::move(xUndoAct));

    m_rController.InvalidateFeature(SID_UNDO);
    m_rController.InvalidateFeature(SID_REDO);

    m_strOrigText = aText;
}

IMPL_LINK_NOARG(OQueryTextView, OnInvalidateTimer, Timer*, void)
{
    m_rController.InvalidateFeature(SID_CUT);
    m_rController.InvalidateFeature(SID_COPY);
    if (!m_bStopTimer)
        m_timerInvalidate.Start();
}

void OQueryTextView::startTimer()
{
    m_bStopTimer = false;
    if (!m_timerInvalidate.IsActive())
        m_timerInvalidate.Start();
}

void OQueryTextView::stopTimer()
{
    m_bStopTimer = true;
    if (m_timerInvalidate.IsActive())
        m_timerInvalidate.Stop();
}

OQueryTextView::~OQueryTextView() { disposeOnce(); }

void OQueryTextView::dispose()
{
    if (m_timerUndoActionCreation.IsActive())
        m_timerUndoActionCreation.Stop();

    m_xSQLEd.reset();
    m_xSQL.reset();
    InterimItemWindow::dispose();
}

void OQueryTextView::GetFocus()
{
    if (m_xSQL)
    {
        m_xSQL->GrabFocus();
        m_strOrigText = m_xSQL->GetText();
    }
    InterimItemWindow::GetFocus();
}

OUString OQueryTextView::getStatement() const { return m_xSQL->GetText(); }

void OQueryTextView::clear()
{
    std::unique_ptr<OSqlEditUndoAct> xUndoAct(new OSqlEditUndoAct(*this));

    xUndoAct->SetOriginalText(m_xSQL->GetText());
    m_rController.addUndoActionAndInvalidate(std::move(xUndoAct));

    SetSQLText(OUString());
}

void OQueryTextView::setStatement(const OUString& rsStatement) { SetSQLText(rsStatement); }

OUString OQueryTextView::GetSQLText() const { return m_xSQL->GetText(); }

void OQueryTextView::SetSQLText(const OUString& rNewText)
{
    if (m_timerUndoActionCreation.IsActive())
    {
        // create the trailing undo-actions
        m_timerUndoActionCreation.Stop();
        OnUndoActionTimer(nullptr);
    }

    m_xSQL->SetTextAndUpdate(rNewText);

    m_strOrigText = rNewText;
}

void OQueryTextView::copy() { m_xSQL->Copy(); }

bool OQueryTextView::isCutAllowed() const { return m_xSQL->HasSelection(); }

void OQueryTextView::cut()
{
    m_xSQL->Cut();
    m_rController.setModified(true);
}

void OQueryTextView::paste()
{
    m_xSQL->Paste();
    m_rController.setModified(true);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */