File: kbufferdrag.cpp

package info (click to toggle)
kdeutils 4%3A3.3.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,628 kB
  • ctags: 13,095
  • sloc: cpp: 95,511; sh: 9,030; perl: 2,786; ansic: 2,425; makefile: 931; lex: 302; xml: 287; yacc: 167
file content (170 lines) | stat: -rw-r--r-- 5,053 bytes parent folder | download
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
/***************************************************************************
                          kbufferdrag.cpp  -  description
                             -------------------
    begin                : Mon Jul 07 2003
    copyright            : (C) 2003 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/

//#include <kdebug.h>

// c specific
#include <string.h>
// lib specific
#include "kbufferdrag.h"
#include "kbordercoltextexport.h"
#include "koffsetcoltextexport.h"
#include "kvaluecoltextexport.h"
#include "kcharcoltextexport.h"

using namespace std;
using namespace KHE;

const char *KBufferDrag::OctetStream = "application/octet-stream";
const char *KBufferDrag::PlainText =   "text/plain";

KBufferDrag::KBufferDrag( const QByteArray &D, KCoordRange Range,
                          const KOffsetColumn *OC, const KValueColumn *HC, const KCharColumn *TC,
                          char SC,
                          QWidget *Source, const char *Name )
  :QDragObject( Source, Name ),
   CoordRange( Range ),
   SubstituteChar( SC )
{
  setData( D );

  Columns = new KColTextExportPtr[5];
  NoOfCol = 0;
  if( HC )
  {
    Columns[NoOfCol++] = new KOffsetColTextExport( OC );
    Columns[NoOfCol++] = new KBorderColTextExport();
    Columns[NoOfCol++] = new KValueColTextExport( HC, Data.data(), CoordRange );
    if( TC )
    {
      Columns[NoOfCol++] = new KBorderColTextExport();
      Columns[NoOfCol++] = new KCharColTextExport( TC, Data.data(), CoordRange );
    }
  }
}


KBufferDrag::KBufferDrag( QWidget *Source, const char *Name )
 : QDragObject( Source, Name )
{
}


KBufferDrag::~KBufferDrag()
{
  for( int i=0; i<NoOfCol; ++i )
    delete Columns[i];
  delete [] Columns;
}


void KBufferDrag::setData( const QByteArray &D )
{
  Data = D;
}


const char *KBufferDrag::format( int i ) const
{
  return( i == 0 ? OctetStream : i == 1 ? PlainText : 0L );
}


QByteArray KBufferDrag::encodedData( const char *Format ) const
{
  if( Format != 0L )
  {
    if( strcmp(Format,OctetStream) == 0 )
    {
//       std::cout << "using " << OctetStream << std::endl;
      return( Data );
    }
    if( strcmp(Format,PlainText) == 0 )
    {
//       std::cout << "using " << PlainText << std::endl;
      QByteArray TextData;
      if( NoOfCol == 0 )
      {
        // duplicate the data and subsitute all non-printable items with a space
        TextData.duplicate( Data );
        char *D = TextData.data();
        for( unsigned int i=0; i<TextData.size(); ++i,++D )
        { unsigned char B = *D; if( B < 32 && B != '\t' && B != '\n' ) *D = SubstituteChar; }
      }
      else
      {
        // initialize: one for the newline \n
        int NeededMemory = 1;
        for( int i=0; i<NoOfCol; ++i )
        {
          NeededMemory += Columns[i]->charsPerLine();
//           cout << "MEM:" << NeededMemory << endl;
        }
        // scale with the number of lines
        NeededMemory *= CoordRange.lines();
//         cout << "FMEM:" << NeededMemory << endl;
        // find out needed size
        /*if( !*/TextData.resize( NeededMemory );// )
//           cout <<"Duh"<<endl;
//         cout << "A" << endl;
        // now fill
        char *D = TextData.data();
        int l = CoordRange.start().line();
//         cout << "startline: "<<l<<endl;
        for( int i=0; i<NoOfCol; ++i )
          Columns[i]->printFirstLine( &D, l );
        *D++ = '\n';
//         cout << "firstline done"<<endl;
        for( ++l; l<=CoordRange.end().line(); ++l )
        {
          for( int i=0; i<NoOfCol; ++i )
            Columns[i]->printNextLine( &D );
          *D++ = '\n';
//           cout << "line"<<l<<" done"<<endl;
        }
//         cout << "done. should:"<<TextData.size()<< " is:"<<D-TextData.data()<<endl;
      }
      return( TextData );
    }
  }

  // return empty dummy
  return QByteArray();
}



bool KBufferDrag::canDecode( const QMimeSource* Source )
{
  bool c =( Source->provides(OctetStream) /*|| Source->provides(PlainText)*/ );
  return c;
//  return( Source->provides(OctetStream) /*|| Source->provides(PlainText)*/ );
}


bool KBufferDrag::decode( const QMimeSource* Source, QByteArray &Dest )
{
//   Dest = Source->encodedData( MediaString );
//   return Dest.size() != 0;

  bool CanDecode = Source->provides( OctetStream );
  if( CanDecode )
    Dest = Source->encodedData( OctetStream );

  return CanDecode;
}

#include "kbufferdrag.moc"