summaryrefslogtreecommitdiffstats
path: root/quanta/messages/messageoutput.cpp
blob: 1abab26370cf24380282ba63e081e602e1b00305 (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
176
177
178
179
/***************************************************************************
                          messageoutput.cpp  -  description
                             -------------------
    begin                : Thu Feb 24 2000
    copyright            : (C) 2000 by Yacovlev Alexander & Dmitry Poplavski <pdima@mail.univ.kiev.ua>
                           (C) 2003-2005 Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// KDE includes
#include <tdeapplication.h>
#include <kdebug.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>

// TQt includes
#include <tqclipboard.h>
#include <tqfile.h>
#include <tqtextstream.h>

#include "messageoutput.h"
#include "messageitem.h"

MessageOutput::MessageOutput(TQWidget *parent, const char *name )
  : TQListBox(parent,name)
{
  m_maxItems = 2000;

  TQPalette pal = palette();
  pal.setColor(TQColorGroup::HighlightedText, pal.color(TQPalette::Active, TQColorGroup::Text));
  pal.setColor(TQColorGroup::Highlight,       pal.color(TQPalette::Active, TQColorGroup::Mid));
  setPalette(pal);
  setFocusPolicy( TQWidget::NoFocus );

  m_popupMenu = new TDEPopupMenu(this);
  connect(this, TQ_SIGNAL(contextMenuRequested(TQListBoxItem*, const TQPoint&)),
     this, TQ_SLOT(showMenu(TQListBoxItem*, const TQPoint&)));
  m_popupMenu->insertItem( SmallIconSet("edit-copy"), i18n("&Copy"), this, TQ_SLOT(copyContent()) ) ;
  m_popupMenu->insertItem( SmallIconSet("document-save-as"), i18n("&Save As..."), this, TQ_SLOT(saveContent()) ) ;
  m_popupMenu->insertSeparator();
  m_popupMenu->insertItem( SmallIconSet("edit-clear"), i18n("Clear"), this, TQ_SLOT(clear()) ) ;

  connect( this, TQ_SIGNAL(clicked(TQListBoxItem*)), TQ_SLOT(clickItem(TQListBoxItem*)) );
}

MessageOutput::~MessageOutput()
{
}

MessageItem *MessageOutput::insertItem(const TQString& s)
{
  checkMaxItems();
  MessageItem *it = new MessageItem(this, s);
  setBottomItem(count()>0?count()-1:0);
  return it;
}

void MessageOutput::addToLastItem(const TQString& s)
{
  int ind = count() - 1;
  if ( ind != -1 ) {
    MessageItem *it = dynamic_cast<MessageItem*>( item(ind) );
    if ( it )
      it->addText( s );
    else
      changeItem( text( ind )+ s, ind );
  }
}

void MessageOutput::showMessage(int line, int col, const TQString &fileName, const TQString& s, bool append)
{
  MessageItem *it = 0L;
  TQString message = s;
  int endPos;
  if ( !count() || (!append && !text(count()-1).stripWhiteSpace().isEmpty()) )
    it = insertItem("");
  while ( ( endPos = message.find('\n') ) != -1 ) {
    if (it)
    {
      it->setLine(line);
      it->setColumn(col);
      it->setFileName(fileName);
    }
    addToLastItem( message.left(endPos) );
    it = insertItem("");
    message.remove(0,endPos+1);
  }
  if (!message.isEmpty())
  {
    if (it)
    {
      it->setLine(line);
      it->setColumn(col);
      it->setFileName(fileName);
    }
    addToLastItem( message);
  }
  setBottomItem(count()>0?count()-1:0);
}

void MessageOutput::showMessage(const TQString& s, bool append)
{
  showMessage(-1, -1, "", s, append);
}


void MessageOutput::checkMaxItems()
{
  if ( count() >= m_maxItems )
    removeItem( index(firstItem()) );
}

void MessageOutput::clickItem( TQListBoxItem * l_item )
{
   MessageItem *item = dynamic_cast<MessageItem*>(l_item);
   if ( item )  {
//     kdDebug(24000) << "Column: " << item->column() << endl;
     if ( item->line() != -1  )
       emit clicked( item->fileName(), item->line() - 1, item->column() - 1);
   }
}

void MessageOutput::showMenu( TQListBoxItem*, const TQPoint& l_point )
{
  m_popupMenu->exec(l_point);
}

TQString MessageOutput::content()
{
  TQString p_content;
  for (uint i=0; i<count(); i++)
    p_content.append(text(i) + "\n");
  return p_content;
}

void MessageOutput::copyContent()
{
  kapp->clipboard()->setText(content(), TQClipboard::Clipboard);
}

void MessageOutput::saveContent()
{
  KURL url=KFileDialog::getSaveURL(TQDir::currentDirPath(),
    i18n("*.log|Log Files (*.log)\n*|All Files"), this, i18n("Save Log File"));
  if (url.isEmpty())
    return;

  TQFileInfo fileinfo(url.path());
  if (fileinfo.exists() && KMessageBox::warningContinueCancel(0,
      i18n("<qt>File<br><b>%1</b><br>already exists. Overwrite it?</qt>")
      .arg(url.path()), TQString(), i18n("Overwrite")) == KMessageBox::Cancel)
    return;

  TQFile file(url.path());
  if (!file.open(IO_WriteOnly)) {
     KMessageBox::error(0, i18n("<qt>Cannot save log file<br><b>%1</b></qt>")
         .arg(url.url()));
     return;
   }

   TQTextStream textfile(&file);
   textfile << content();
   file.close();

}


#include "messageoutput.moc"